PC <-> ZX link sourse (PC)
ZXNet echo conference «code.zx»
From Aleksandr Majorov → To Dmitriy Dobretsov 7 January 1999
Пpиветствую тебя, о дpажайший(ая/ое) Dmitriy!
Once in morning 07-01-98 04:18:53 somebody Dmitriy Dobretsov has
written to Aleksandr Majorov about 'Гальваническая развязка'.
[ ]
AM>> если интересно могу сорцы кинуть в .code, покажешь
AM>> где можно оптимизнуть..
DD> кинь,если не влом...
Софтовая поддержка для ПЦ
=================================================
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
char ver[]="-= PC-ZX NULL-modem link v1.00 =-";
static unsigned char buff[65535]; // буфер файла
unsigned char data[262], out[262], d; // входной и выходной буфера
char *fbuff;
int *inhob = (int*) &data[18];
int *outhob = (int*) &buff[15];
int *outlen = (int*) &buff[11];
int *outcrc = (int*) &out[260];
unsigned int leno, len, flag=0;
int hobeta=1, erase=0;
// ---------------------------------------------------------------
void out_info();
void out_ver();
int rec_data();
int trans_1(int );
int trans_data();
int scan_in();
int scan_in_();
int zx_out(int );
int zx_in();
int wait_0();
int wait_1();
int out_comm(int , int );
int out_block();
int long_wait(int );
void sbreak(char *);
void print(char *);
int out_X_MODEM_block(int );
int crc_data(char *);
int chr_corr(int );
unsigned short CRC_hobeta(char *);
void in_file();
int wait_block(int blk);
int transm_file(char *);
int tr_wait0();
int load_file(char *);
void make_hobeta(char *, unsigned int);
void out_file(char *);
void out_cat();
void print_command(char *);
void use_comm(char *);
void help();
int sys_wait();
void wait_no_data();
//************************************************************
//* ОСHОВHОЙ МОДУЛЬ ПРОГРАММЫ *
//************************************************************
void main(int argc, char *argv[]){
int i;
outp(890,12); // init LPT //
delay(50);
outp(890,8);
clrscr();
printf("
╔═══════════════════════════════════╗");
printf("
║ ║");
printf("
║ %s ║", ver);
printf("
║ ║");
printf("
╚═══════════════════════════════════╝
");
print_command("start");
for(i=1; i1 && argv[i][1]=='_') use_comm(argv[i]);
}
while(1){
if ( rec_data()){
if( len==1 && data[0]==0x0F) out_ver();
if( len==5 && data[0]==0x1B && data[1] == 'd' ) in_file();
if( len>2){
if( data[0]==21 && data[2] == ' ' ) use_comm(&data[1]);
}
}
if (kbhit()) if (getch() == 32) break;
}
print_command("End programm
");
exit(0);
}
//**********************************
// ожидание данных. Возвращается 1 если ничего не принято
int rec_data(){
int i;
if ( scan_in() ) return 0;
if ( zx_in() || (d != 0xAA) ) return 0;
if ( zx_in() || (d != 0x55) ) return 0;
if ( zx_in() ) return 0;
len = d;
if ( zx_in() ) return 0;
len = d * 256 + len;
for (i=0; i 32767){
crc = crc << 1;
crc = crc ^ 0x1021;
}
else crc = crc << 1;
b = b << 1;
}
}
return crc;
}
//***************************************
int chr_corr(int p){ // коррекция MS-DOS имени - запрещенные символы
if(p==' ' || p=='"' || p=='.' || p=='//') return '_';
if(p > ')' && p < '-') return '_';
if(p > '9' && p < '@') return '_';
if(p > 'Z' && p < '_') return '_';
if(p > 'z' && p < 'А') return '_';
return p;
}
//************************************************************
//* Вывести информацию о программе и статус переключателей *
//************************************************************
void out_ver(){
print_command("ask version");
strcpy(out, ver);
strcat(out, "
Текущий каталог:
");
strcat(out, getcwd(data, 200));
strcat(out, "
Принимать файлы как ");
if(hobeta) strcat(out, "Hobeta");
else strcat(out, "текст");
strcat(out, ".
Переданные файлы ");
if(erase==0) strcat(out, "не ");
strcat(out, "удалять.
");
leno=strlen(out);
delay(100);
trans_data();
}
void out_info(){
strcpy(out, "H: ");
if(hobeta) strcat(out, "On ");
else strcat(out, "Off");
strcat(out, "
D: ");
if(erase) strcat(out, "On ");
else strcat(out, "Off");
strcat(out, "
C: ");
strcat(out, getcwd(data, 200));
strcat(out, "
");
leno=strlen(out);
delay(100);
trans_data();
}
//************************************************************
//* Прием файлов *
//************************************************************
void in_file(){
unsigned int filelen, filesec, ib, i, CRC, handle;
unsigned char c, name[13];
time_t t;
print_command("in file(s)");
printf("
Число файлов=%i", data[3]);
for(i=0; i<10; i++) trans_1(0x16); //sync
while(1){
if( wait_block(1) ) return;
t = time(NULL);
if ( *inhob != CRC_hobeta(&data[3]) ){
sbreak("нет заголовка HOBETA!");
return;
}
filelen = data[15]*256+data[14];
filesec = data[17];
printf("
file: "); //вывести имя ТР-ДОС файла
for(i=0; i<8; i++) printf("%c", data[i+3]);
printf("<%c> %3u ", data[i+3], data[17]);
for(i=0, ib=0; i<8; i++) name[i] = chr_corr(data[i+3]);
name[i++] = '.';
name[10] = ' ';
name[11] = ' ';
name[12] = '';
if(hobeta){
fbuff = &buff[0];
name[i++] = '$';
filelen = 17+filesec*256;
}
else fbuff = &buff[17];
name[i] = chr_corr(data[11]);
//-------------------------------------------
memcpy(&buff[ib], &data[3], 256);
ib += 256;
// принимаем блоки со 2-го по предпоследний
for(c=1; c < filesec; c++){
if( wait_block(c+1) ) return;
memcpy(&buff[ib], &data[3], 256);
ib += 256;
putch(8);
putch(8);
putch(8);
printf("%3i", c);
}
putch(8);
putch(8);
putch(8);
// ожидаем последний блок, в нем только 17 значимых байт
if( wait_block(++c) ) return;
memcpy(&buff[ib], &data[3], 17);
// принимаем блок окончания - один байт
if( wait_block(-1) ) return;
/* проверяем на совпадающие файлы.
Если файл с таким именем есть - увеличить последний символ
расширания на 1 */
i = 10 + (hobeta==1); //2-й или 3-й символ (.C или .$C)
while(1){
if( access(name, 0) != 0) break;
name[i]++;
if(name[i]=='!') name[i] = '0';
if(name[i]==':') name[i] = 'a';
if(name[i]=='{'){
sbreak("невозможно создать файл!");
return;
}
}
printf(" MS-DOS name: %s %u", name, filelen);
handle = open(name, O_CREAT|O_TRUNC|O_BINARY, S_IWRITE);
write (handle, fbuff, filelen);
close (handle);
trans_1(0x06);
delay(50);
trans_1(0x06);
delay(100);
trans_1(0x06);
printf(" ok. %ld сек", time(NULL)-t);
}
}
=================================================
Hу вот и все *MAS* с кувалдой
From Aleksandr Majorov → To Dmitriy Dobretsov 7 January 1999
Greetings, oh dearest Dmitriy!
Once in morning 07-01-98 04:18:53 somebody Dmitriy Dobretsov has
written to Aleksandr Majorov about 'Galvanic isolation'.
[ ]
AM>> if you're interested, I can throw the sources into the .code and show you
AM>> where you can optimize..
DD> throw it if it doesn't break...
====================================================
//*************************************************
int wait_block(int blk){ //wait for block number blk to be received
int i, r, c;
c=0x06; //block_ok;
for(i=1; i<20; i++){
if( blk==1 ) c = 0x43; // if we accept the 1st block, issue a request
trans_1(c);
r = long_wait(i);
if(r==0) return 1; //error
if(r==3){
sbreak("End receive");
return 1;
}
c = 0x15; //block_err;
if(r==1){
if(blk==-1 && data[0]==0x04) return 0; //end file
if(len==261 && data[0]==1 && ((data[1]^data[2])==255)){
if( (data[259]*256+data[260]) == crc_data(data) ){
if( data[1] == blk ) return 0;
if( data[1] < blk ) c = 0x06; //block ok, but this is the previous one
}
}
}
}
sbreak("error");
return 1;
}
//****************************************************************
//* Transferring a file loaded into the buffer *//************************************************************
int transm_file(char *p){
unsigned int c, ib;
unsigned char i;
time_t t;
if( tr_wait0() ) return 1; // нет запросов приемника
if(strlen(p)){
printf("
MS-DOS file: ");
fnsplit(p, NULL, NULL, data, out);
printf("%s", data);
if(strlen(data)<8) for(i=0; i<(8-strlen(data)); i++) printf(" ");
printf("%s", out);
if(strlen(out)<4) for(i=0; i<(4-strlen(out)); i++) printf(" ");
}
printf(" ");
for(i=0; i<8; i++) printf("%c", buff[i]); //вывести имя ТР-ДОС файла
printf("<%c> %3u ", buff[i], buff[14]);
t = time(NULL);
for(i=0, ib=0; i65297){
sbreak("File too long or No file");
return 1;
}
/* read the 1st 17 bytes, if this is a HOBETA header, read the entire file
otherwise, create a HOBETA header and read the file at offset +17 */
read(handle, &buff[0], 17);
close(handle);
if ( *outhob == CRC_hobeta(&buff[0]) ) i = 0;
else{
make_hobeta(p, filelen);
i = 17;
}
handle = open(p, O_BINARY);
read(handle, &buff[i], filelen);
close(handle);
return 0;
}
// create a HOBETA file header named *p
void make_hobeta(char *p, unsigned int filelen){
int i, j, CRC;
setmem(&buff[0], 17, ' ');
for(i=j=0; i<12; i++) if(p[i] > ' ') buff[j++]=p[i];
buff[8] = 'C';
buff[9] = 0;
buff[10] = 0;
*outlen = filelen;
buff[13] = 0;
buff[14] = (buff[12]) + (buff[11]!=0);
*outhob = CRC_hobeta(&buff[0]);
}//************************************************************
//* ПЕРЕДАЧА ФАЙЛОВ ПО МАСКЕ *
//************************************************************
void out_file(char *mask){
struct ffblk ffblk;
unsigned char t[129][13], name[9], ext[5];
unsigned int done, i, j, f;
// ищем файлы, попадающие по маске
f = 0;
done = findfirst(mask, &ffblk, 0);
while(!done && f<128){
if(ffblk.ff_fsize!=0 && ffblk.ff_fsize<65298){
fnsplit(ffblk.ff_name, NULL, NULL, name, ext);
strcpy(t[f], name);
strcat(t[f], ext);
f++;
}
done = findnext(&ffblk);
}
if(f==0){
print("No files");
return;
}
// сортируем по возрастанию имени файла
for(i=0; i<(f-1); i++){
for(j=0; j<(f-1); j++){
if(strcmp(t[j], t[j+1]) > 0){
strcpy(t[128], t[j]);
strcpy(t[j], t[j+1]);
strcpy(t[j+1], t[128]);
}
}
}
printf("
Маска=%s Число файлов=%i", mask, f);
if( out_comm('d