-
Notifications
You must be signed in to change notification settings - Fork 4
/
SDrive.c
2231 lines (1893 loc) · 67.1 KB
/
SDrive.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//*****************************************************************************
// SDrive
// Bob!k & Raster, C.P.U., 2008-2012
//*****************************************************************************
#include <avr/io.h> // include I/O definitions (port names, pin names, etc)
#include <avr/interrupt.h> // include interrupt support
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include "avrlibdefs.h" // global AVRLIB defines
#include "avrlibtypes.h" // global AVRLIB types definitions
#include "spi.h" // include SPI interface functions
#include "mmc.h" // include MMC card access functions
#include "fat.h"
#include "sboot.h"
#define DEBUG_RAM 0 //0=off, 1=on
#define TWOBYTESTOWORD(ptr) *((u16*)(ptr))
#define FOURBYTESTOLONG(ptr) *((u32*)(ptr))
extern void Delay100usX(unsigned short delay); //externi funkce
extern void Delay100us();
extern void Delay200us();
extern void Delay800us();
extern void Delay1000us();
//F_CPU = 14318180
//UBRR = (F_CPU/16/BAUD)-1
//BAUD = (F_CPU/16/(UBRR+1)
//Pokey_frq = 1789790
//BAUD = Pokey_frq/2/(AUDF+7) //(16bit register)
#define US_POKEY_DIV_STANDARD 0x28 //#40 => 19040 bps
#define ATARI_SPEED_STANDARD (US_POKEY_DIV_STANDARD+6) //#46 (o sest vic)
#define US_POKEY_DIV_DEFAULT 0x06 //#6 => 68838 bps
#define US_POKEY_DIV_MAX (255-6) //pokeydiv 249 => avrspeed 255 (vic nemuze)
/*
#define SIOSPEED_MODES 9 //pocet fastsio_mode=0..8
#define US_POKEY_DIV_DEFAULT US_POKEY_DIV_7
#define ATARI_SPEED_DEFAULT ATARI_SPEED_7
//sio 1x standard
#define US_POKEY_DIV_STANDARD 0x28 //=> 19040 bps
#define ATARI_SPEED_STANDARD 46 // normal speed
//=46 => F_CPU/16/(46+1) = 19040
//sio2x
#define US_POKEY_DIV_2 0x10 //=> 38908 bps
#define ATARI_SPEED_2 22 // sio2x speed
//=22 => F_CPU/16/(22+1) = 38908
//Happy
#define US_POKEY_DIV_3 0x0a //=> 52641 bps
#define ATARI_SPEED_3 16 // happy
//=16 => F_CPU/16/(16+1) = 52640
//Speedy
#define US_POKEY_DIV_4 0x09 //=> 55931 bps
#define ATARI_SPEED_4 15 // speedy TOTO FUNGUJE
//=15 => F_CPU/16/(15+1) = 55930
//sio3x
#define US_POKEY_DIV_5 0x08 //=> 59660 bps
#define ATARI_SPEED_5 14 // sio3x speed
//=14 => F_CPU/16/(14+1) = 59659
//sio 64000
#define US_POKEY_DIV_6 0x07 //=> 63921 bps
#define ATARI_SPEED_6 13 // sio3x speed
//=13 => F_CPU/16/(13+1) = 63920
// works great!
#define US_POKEY_DIV_7 0x06 //=> 68838 bps
#define ATARI_SPEED_7 12 // ULTRA speed - closest match for both systems (AVR,ATARI)
//=12 => F_CPU/16/(12+1) = 68837
//sio 75000
#define US_POKEY_DIV_8 0x05 //=> 74575 bps
#define ATARI_SPEED_8 11 // sio4x speed
//=11 => F_CPU/16/(11+1) = 74574
*/
#define FILE_ACCESS_READ 0
#define FILE_ACCESS_WRITE 1
// Aktivita
#define LED_GREEN_ON cbi(PORTC,5)
#define LED_GREEN_OFF sbi(PORTC,5)
// Zapis
#define LED_RED_ON cbi(PORTC,4)
#define LED_RED_OFF sbi(PORTC,4)
unsigned char mmc_sector_buffer[512]; // one SD sector
u32 n_actual_mmc_sector;
unsigned char n_actual_mmc_sector_needswrite;
unsigned char atari_sector_buffer[256];
#define FileFindBuffer (atari_sector_buffer+256-11) //pri vyhledavani podle nazvu
struct GlobalSystemValues GS;
struct FileInfoStruct FileInfo; //< file information for last file accessed
extern u32 debug_endofvariables;
#define send_ACK() USART_Transmit_Byte('A');
#define send_NACK() USART_Transmit_Byte('N');
#define send_CMPL() USART_Transmit_Byte('C');
#define send_ERR() USART_Transmit_Byte('E');
#define wait_cmd_HL() { while ( (inb(PIND) & (1<<PIND4)) ); }
#define wait_cmd_LH() { while ( !(inb(PIND) & (1<<PIND4)) ); }
#define CMD_STATE_H (1<<PIND4)
#define CMD_STATE_L 0
#define get_cmd_H() ( inb(PIND) & (1<<PIND4) )
#define get_cmd_L() ( !(inb(PIND) & (1<<PIND4)) )
#define get_readonly() ( inb(PIND) & (1<<PIND3) ) //read only
#define read_key() (inb(PIND)&0xe4)
unsigned char last_key;
unsigned char get_checksum(unsigned char* buffer, u16 len)
{
u16 i;
u08 sumo,sum;
sum=sumo=0;
for(i=0;i<len;i++)
{
sum+=buffer[i];
if(sum<sumo) sum++;
sumo = sum;
}
return sum;
}
/*
//pouziva se jen jednou v USART_Init, takze vlozena primo do teto funkce (uspora 10bytu)
void USART_Flush( void )
{
unsigned char dummy;
while ( UCSRA & (1<<RXC) ) dummy = UDR;
}
*/
void USART_Init( u08 value )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) ); //cekani
/* Set baud rate */
// UBRRH = (unsigned char)(baud>>8);
// UBRRL = (unsigned char)(baud&0xff);
UBRRH = 0; //HB =0
UBRRL = value;
/* Set double speed flag */
// UCSRA = (1<<UDRE)|(1<<U2X); //double speed
UCSRA = (1<<UDRE);
/* Enable Receiver and Transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 1stop bit */
UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
//void USART_Flush( void )
{
unsigned char dummy;
while ( UCSRA & (1<<RXC) ) dummy = UDR;
}
}
void USART_Transmit_Byte( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) ) ;
/* Put data into buffer, sends the data */
UDR = data;
}
unsigned char USART_Receive_Byte( void )
{
/* Wait for data to be received */
while ( !(UCSRA & (1<<RXC)) );
/* Get and return received data from buffer */
return UDR;
}
/*
//Bob rekl ze zapoznamkovat - nikde se nepouziva 25.1.2008
unsigned char USART_Is_Byte_Received( void )
{
return (UCSRA & (1<<RXC))?1:0;
}
*/
void USART_Send_Buffer(unsigned char *buff, u16 len)
{
while(len>0) { USART_Transmit_Byte(*buff++); len--; }
}
/*
void USART_Get_Buffer(unsigned char *buff, u16 len)
{
while(len>0) { *buff++=USART_Receive_Byte(); len--; }
}
*/
u08 USART_Get_Buffer_And_Check(unsigned char *buff, u16 len, u08 cmd_state)
{
//prebere len bytu + 1 byte checksum
//pokud se behem nacitani zmeni cmd_state => return 0x01
//pokud se behem nacitacni zmeni getkey => return 0x02
//pak zkontroluje checksum
//pokud nesouhlasi checksum, return 0x80
//pokud vse ok, return 0x00
u08 *ptr;
u16 n;
u08 b;
ptr=buff;
n=len;
while(1)
{
// Wait for data to be received
do
{
//pokud by prisel command L nebo stisknuta klavesa, prerusi prijem
if ( get_cmd_H()!=cmd_state ) return 0x01;
if ( read_key()!=last_key ) return 0x02;
} while ( !(UCSRA & (1<<RXC)) );
// Get and return received data from buffer
//return UDR;
b=UDR;
if (!n)
{
//v b je checksum (n+1 byte)
if ( b!=get_checksum(buff,len) ) return 0x80; //chyba checksumu
return 0x00; //ok
}
*ptr++=b;
n--;
}
}
u08 USART_Get_buffer_and_check_and_send_ACK_or_NACK(unsigned char *buff, u16 len)
{
unsigned char err;
//tady pred ctenim zadna pauza
err = USART_Get_Buffer_And_Check(buff,len,CMD_STATE_H);
//vraci 0 kdyz ok, !=0 kdyz chyba
Delay1000us(); //t4
if(err)
{
send_NACK();
return 0xff; //kdyz nesouhlasi checksum
}
send_ACK();
// Delay300us(); //po ACKu je pauza (250usec az 255s), protoze se pak posila CMPL nebo ERR
// ^ tahle pauza se dela pred CMPL nebo ERR
return 0; //kdyz je ok vraci 0
}
/*
u08 USART_Get_atari_sector_buffer_and_check_and_send_ACK_or_NACK(u16 len)
{
USART_Get_buffer_and_check_and_send_ACK_or_NACK(atari_sector_buffer,len);
}
*/
#define USART_Get_atari_sector_buffer_and_check_and_send_ACK_or_NACK(len) USART_Get_buffer_and_check_and_send_ACK_or_NACK(atari_sector_buffer,len)
void USART_Send_cmpl_and_atari_sector_buffer_and_check_sum(unsigned short len)
{
u08 check_sum;
// Delay300us(); //po ACKu pred CMPL pauza 250us - 255sec
// Delay300us(); //po ACKu pred CMPL pauza 250us - 255sec
//Kdyz bylo jen 300us tak nefungovalo
Delay800us(); //t5
send_CMPL();
// Delay1000us(); //S timhle to bylo doposud (a nefunguje pod Qmegem3 Ultraspeed vubec)
// Delay100us(); //Pokusne kratsi pauza
//(tato fce se pouziva se i u read SpeedIndex 3F,
// coz s delsi pauzou nefunguje (pod Qmegem3 fast)
//Delay800us(); //t6
Delay200us(); //<--pouziva se i u commandu 3F
USART_Send_Buffer(atari_sector_buffer,len);
check_sum = get_checksum(atari_sector_buffer,len);
USART_Transmit_Byte(check_sum);
}
u08 mmcWriteCached(unsigned char force);
void mmcWriteCachedFlush();
void mmcReadCached(u32 sector)
{
if(sector==n_actual_mmc_sector) return;
u08 ret,retry;
//predtim nez nacte jiny, musi ulozit soucasny
mmcWriteCachedFlush();
//az ted nacte novy
retry=0; //zkusi to maximalne 256x
do
{
ret = mmcRead(sector); //vraci 0 kdyz ok
retry--;
} while (ret && retry);
while(ret); //a pokud se vubec nepovedlo, tady zustane zablokovany cely SDrive!
n_actual_mmc_sector=sector;
}
u08 mmcWriteCached(unsigned char force)
{
if ( get_readonly() ) return 0xff; //zakazany zapis
LED_RED_ON;
if (force)
{
u08 ret,retry;
retry=16; //zkusi to maximalne 16x
do
{
ret = mmcWrite(n_actual_mmc_sector); //vraci 0 kdyz ok
retry--;
} while (ret && retry);
while(ret); //a pokud se vubec nepovedlo, tady zustane zablokovany cely SDrive!
n_actual_mmc_sector_needswrite = 0;
LED_RED_OFF;
}
else
{
n_actual_mmc_sector_needswrite=1;
}
return 0; //vraci 0 kdyz ok
}
void mmcWriteCachedFlush()
{
if (n_actual_mmc_sector_needswrite)
{
while (mmcWriteCached(1)); //pokud je zakazany zapis, tady zustane zablokovany cely SDrive!
//do okamziku, nez ten zapis nebude povolen (visi mu neco v cache)
}
}
unsigned short getClusterN(unsigned short ncluster)
{
if(ncluster<FileInfo.vDisk.ncluster)
{
FileInfo.vDisk.current_cluster=FileInfo.vDisk.start_cluster;
FileInfo.vDisk.ncluster=0;
}
while(FileInfo.vDisk.ncluster!=ncluster)
{
FileInfo.vDisk.current_cluster=nextCluster(FileInfo.vDisk.current_cluster);
FileInfo.vDisk.ncluster++;
}
return (FileInfo.vDisk.current_cluster&0xFFFF);
}
unsigned short faccess_offset(char mode, unsigned long offset_start, unsigned short ncount)
{
unsigned short j;
unsigned long offset=offset_start;
unsigned long end_of_file;
unsigned long ncluster, nsector, current_sector;
unsigned long bytespercluster=((u32)SectorsPerCluster)*((u32)BytesPerSector);
if(offset_start>=FileInfo.vDisk.size)
return 0;
end_of_file = (FileInfo.vDisk.size - offset_start);
ncluster = offset/bytespercluster;
offset-=ncluster*bytespercluster;
nsector = offset/((u32)BytesPerSector);
offset-=nsector*((u32)BytesPerSector);
getClusterN(ncluster);
current_sector = fatClustToSect(FileInfo.vDisk.current_cluster) + nsector;
mmcReadCached(current_sector);
for(j=0;j<ncount;j++,offset++)
{
if(offset>=((u32)BytesPerSector) )
{
if(mode==FILE_ACCESS_WRITE && mmcWriteCached(0))
return 0;
offset=0;
nsector++;
if(nsector>=((u32)SectorsPerCluster) )
{
nsector=0;
ncluster++;
getClusterN(ncluster);
}
current_sector = fatClustToSect(FileInfo.vDisk.current_cluster) + nsector;
mmcReadCached(current_sector);
}
if(!end_of_file)
break; //return (j&0xFFFF);
if(mode==FILE_ACCESS_WRITE)
mmc_sector_buffer[offset]=atari_sector_buffer[j]; //SDsektor<-atarisektor
else
atari_sector_buffer[j]=mmc_sector_buffer[offset]; //atarisektor<-SDsektor
end_of_file--;
}
if(mode==FILE_ACCESS_WRITE && mmcWriteCached(0))
return 0;
return (j&0xFFFF);
}
// DISPLAY
#define init_display() { PORTC=(inb(PINC)&0xc0)|0x3f; } //zhasne vsechny kontrolky
void set_display(unsigned char n)
{
//PORTC=(inb(PINC)&0xe0)|(0x1f ^ (1<<(4-n)) );
//PORTC=(inb(PINC)&0xf0)|( (n>0)? (0x0f ^ (1<<(4-n)) ) : 0);
u08 v;
v=inb(PINC)&0xf0;
if (n>0) v|=(0x0f ^ (1<<(4-n)) ); //pro bootD0: sviti vsechny 4 zlute diody
PORTC=v;
}
//#define full_display() { PORTC=(inb(PINC)&0xc0)|0x00; }
//prog_uchar system_atr_name[]={"SDRIVE .ATR"};
// =pgm_read_byte_near(&system_atr_name[idx]);
//uint8_t EEMEM system_atr_name[]={"SDRIVE ATR"};
// =eeprom_read_byte(&system_atr_name[idx]);
//
//uint8_t EEMEM system_info[]="SDrive01 20081012 Bob!k & Raster, C.P.U."; //SDriveVersion info
uint8_t EEMEM system_info[]="SDrive02 20120901 Bob!k & Raster, C.P.U."; //SDriveVersion info
// VVYYYYMMDD
// VV cislo nove oficialne vydane verze, meni se jen pri vydani noveho oficialniho firmware
// s rozsirenymi/zmenenymi funkcemi zpetne nekompatibilni
// rostouci posloupnost 01 .. 09 0A 0B .. 0Z .. 0a 0b .. 0y 0z 10 11 ..... zz
//nasledujici parametry se daji modifiovat pres EEPROM configure
// |filenameext|
uint8_t EEMEM system_atr_name[]="SDRIVE ATR"; //8+3 zamerne deklarovano za system_info,aby bylo pripadne v dosahu pres get status
//
uint8_t EEMEM system_fastsio_pokeydiv_default=US_POKEY_DIV_DEFAULT;
/*
1) 720*128=92160 ( 90,000KB)
2) 1040*128=133120 ( 130,000KB)
3) 3*128+717*256=183936 ( 179,625KB)
4) 3*128+1437*256=368256 ( 359,625KB)
5) 3*128+2877*256=736896 ( 719,625KB)
6) 3*128+5757*256=1474176 (1439,625KB)
7) 3*128+11517*256=2948736 (2879,625KB)
8) 3*128+...
*/
#define IMSIZE1 92160
#define IMSIZE2 133120
#define IMSIZE3 183936
#define IMSIZE4 368256
#define IMSIZE5 736896
#define IMSIZE6 1474176
#define IMSIZE7 2948736
#define IMSIZES 7
uint8_t EEMEM system_percomtable[]= {
0x28,0x01,0x00,0x12,0x00,0x00,0x00,0x80, IMSIZE1&0xff,(IMSIZE1>>8)&0xff,(IMSIZE1>>16)&0xff,(IMSIZE1>>24)&0xff,
0x28,0x01,0x00,0x1A,0x00,0x04,0x00,0x80, IMSIZE2&0xff,(IMSIZE2>>8)&0xff,(IMSIZE2>>16)&0xff,(IMSIZE2>>24)&0xff,
0x28,0x01,0x00,0x12,0x00,0x04,0x01,0x00, IMSIZE3&0xff,(IMSIZE3>>8)&0xff,(IMSIZE3>>16)&0xff,(IMSIZE3>>24)&0xff,
0x28,0x01,0x00,0x12,0x01,0x04,0x01,0x00, IMSIZE4&0xff,(IMSIZE4>>8)&0xff,(IMSIZE4>>16)&0xff,(IMSIZE4>>24)&0xff,
0x50,0x01,0x00,0x12,0x01,0x04,0x01,0x00, IMSIZE5&0xff,(IMSIZE5>>8)&0xff,(IMSIZE5>>16)&0xff,(IMSIZE5>>24)&0xff,
0x50,0x01,0x00,0x24,0x01,0x04,0x01,0x00, IMSIZE6&0xff,(IMSIZE6>>8)&0xff,(IMSIZE6>>16)&0xff,(IMSIZE6>>24)&0xff,
0x50,0x01,0x00,0x48,0x01,0x04,0x01,0x00, IMSIZE7&0xff,(IMSIZE7>>8)&0xff,(IMSIZE7>>16)&0xff,(IMSIZE7>>24)&0xff,
0x01,0x01,0x00,0x00,0x00,0x04,0x01,0x00, 0x00,0x00,0x00,0x00
};
#define DEVICESNUM 5 // //D0:-D4:
static virtual_disk_t vDisk[DEVICESNUM];
virtual_disk_t* GetvDiskPtr(u08 devicenum)
{
return &vDisk[devicenum];
}
void Clear_atari_sector_buffer(u16 len)
{
//Maze atari_sector_buffer
u08 *ptr;
ptr=atari_sector_buffer;
do { *ptr++=0; len--; } while (len);
}
//#define Clear_atari_sector_buffer_256() Clear_atari_sector_buffer(256)
void Clear_atari_sector_buffer_256()
{
Clear_atari_sector_buffer(256); //0 => maze 256 bytu
}
void PutDecimalNumberToAtariSectorBuffer(unsigned char num, unsigned char pos)
{
u08 *ptr;
ptr = (atari_sector_buffer+pos);
*ptr++=0x30|(num/10);
*ptr=0x30|(num%10);
}
struct SDriveParameters
{
u08 p0;
u08 p1;
u08 p2;
u08 p3;
u32 p4_5_6_7;
};
//sdrparams je nadeklarovano uvnitr main() (hned na zacatku)
#define actual_drive_number sdrparams.p0
#define fastsio_active sdrparams.p1
#define fastsio_pokeydiv sdrparams.p2
#define bootloader_relocation sdrparams.p3
#define extraSDcommands_readwritesectornumber sdrparams.p4_5_6_7
//----- Begin Code ------------------------------------------------------------
int main(void) __attribute__ ((noreturn)); //size optimization 8122->8098 bytes
int main(void)
{
unsigned char command[5];
unsigned char last_used_virtual_drive;
//Parameters
struct SDriveParameters sdrparams;
#if DEBUG_RAM
{
//naplni pamet 0x55kama
unsigned char *p;
for(p=0x60; p<(0x045f-64); p++) *p=0x55;
}
debug_endofvariables=0xaaaaaaaa; //posledni promenna
#endif
/*
sbi(DDRC,0); // LED_pin out;
sbi(DDRC,1); // LED_pin out;
sbi(DDRC,2); // LED_pin out;
sbi(DDRC,3); // LED_pin out;
sbi(DDRC,4); // LED_pin out;
sbi(DDRC,5); // LED_pin out;
*/
DDRC |= 0x3f; //00111111
cbi(DDRB,0); // KEY_pin input
cbi(DDRB,1); // KEY_pin input
/*
cbi(DDRD,2); // KEY_pin input
cbi(DDRD,3); // KEY_pin input
cbi(DDRD,4); // KEY_pin input (karta)
cbi(DDRD,5); // KEY_pin input
cbi(DDRD,6); // KEY_pin input
cbi(DDRD,7); // KEY_pin input
*/
DDRD &= ~0xfc; //11111100
sbi(PORTB,0); // pull-up
sbi(PORTB,1); // pull-up
/*
sbi(PORTD,2); // pull-up
sbi(PORTD,3); // pull-up
sbi(PORTD,4); // pull-up (karta)
sbi(PORTD,5); // pull-up
sbi(PORTD,6); // pull-up
sbi(PORTD,7); // pull-up
*/
PORTD |= 0xfc; //11111100
SD_CARD_EJECTED:
last_key=0;
last_used_virtual_drive=0; //inicializace
fastsio_active=0;
bootloader_relocation=0;
//actual_drive_number=0; //nastavovano kousek dal (za SET_BOOT_DRIVE)
FileInfo.percomstate=0; //inicializace
//fastsio_pokeydiv=US_POKEY_DIV_DEFAULT; //default fastsio
fastsio_pokeydiv=eeprom_read_byte(&system_fastsio_pokeydiv_default); //definovano v EEPROM
init_display();
USART_Init(ATARI_SPEED_STANDARD);
LED_GREEN_ON; // LED on
//CEKACI SMYCKA!!!
while (inb(PIND)&0x04); //cekani az zasune nejakou kartu
//pozor, v tomto okamziku nesmi byt nacacheovan zadny sektor
//pro zapis, protoze init karty inicializuje i mmc_sector atd.!!!
do
{
mmcInit();
}
while(mmcReset()); //dokud nenulove, tak smycka (return 0 => ok!)
//n_actual_mmc_sector=0xFFFFFFFF; //presunuto do mmcReset
// set SPI speed on maximum (F_CPU/2)
sbi(SPSR, SPI2X);
cbi(SPCR, SPR1);
cbi(SPCR, SPR0);
{
u08 i;
for(i=0; i<DEVICESNUM; i++) GetvDiskPtr(i)->flags=0; //vDisk[i].flags=0;
}
FileInfo.vDisk.flags=0; //special_xex_loader=0;
SET_BOOT_DRIVE:
actual_drive_number=0; //pro bootovani z jednotky vD0:
SET_SDRIVEATR_TO_D0: //pro nastaveni SDRIVE.ATR do vD0: bez zmeny actual_drive_number !
//uschovani aktualne vybraneho drive (po opakovanem stlaceni Boot tlacitka)
//protoze obsah vD0: bude menit
if ( last_used_virtual_drive != 0 )
{
//chce pracovat s jinym vDx nez se pracovalo posledne
unsigned char i;
unsigned char *p1;
unsigned char *p2;
p2=((unsigned char *)&FileInfo.vDisk);
if ( last_used_virtual_drive < DEVICESNUM )
{
p1=((unsigned char *)&vDisk[last_used_virtual_drive]);
// vDisk[last_used_virtual_drive] <- vDisk
for(i=0; i<sizeof(virtual_disk_t); i++) p1[i]=p2[i];
}
last_used_virtual_drive=0; //bude menit vD0:
}
if ( !fatInit() ) //Inicializace FATky
{
//ta karta co je tam zastrcena neni zadny
//filesystem co umime
//takze zhasneme zelenou diodu
LED_GREEN_OFF; // LED OFF
//a pocka az vytahnes kartu ven
while (!(inb(PIND)&0x04)); //cekani az vysune kartu ven
//a jde cekat na jinou
goto SD_CARD_EJECTED;
}
//Vyhledani a nastaveni image SDRIVE.ATR do vD0:
{
unsigned short i;
unsigned char m;
i=0;
while( fatGetDirEntry(i,0) )
{
for(m=0;m<11;m++) //8+3
{
if(atari_sector_buffer[m]!=eeprom_read_byte(&system_atr_name[m]))
{
//odlisny znak
goto find_sdrive_atr_next_entry;
}
}
//Slava, nasel SDRIVE.ATR
FileInfo.vDisk.current_cluster=FileInfo.vDisk.start_cluster;
FileInfo.vDisk.ncluster=0;
//FileInfo.vDisk.file_index = i; //dela se uvnitr fatGetDirEntry
faccess_offset(FILE_ACCESS_READ,0,16); //ATR hlavicka vzdy
FileInfo.vDisk.flags=FLAGS_DRIVEON;
if ( (atari_sector_buffer[4]|atari_sector_buffer[5])==0x01 )
FileInfo.vDisk.flags|=FLAGS_ATRDOUBLESECTORS;
{
unsigned long compute;
unsigned long tmp;
compute = FileInfo.vDisk.size - 16; //je to vzdy ATR
tmp = (FileInfo.vDisk.flags & FLAGS_ATRDOUBLESECTORS)? 0x100:0x80;
compute /=tmp;
if(compute>720) FileInfo.vDisk.flags|=FLAGS_ATRMEDIUMSIZE;
}
goto find_sdrive_atr_finished;
//
find_sdrive_atr_next_entry:
i++;
} //end while
//nenasel SDRIVE.ATR
FileInfo.vDisk.flags=0; //ve vD0: neni aktivni disk
//takze nastavi jednotku dle cisla SDrive (1-4)
if (actual_drive_number==0)
{
actual_drive_number=(unsigned char)4-((unsigned char)(inb(PINB))&0x03);
}
}
find_sdrive_atr_finished:
set_display(actual_drive_number);
/////////////////////
for(;;)
{
ST_IDLE:
LED_GREEN_OFF; // LED OFF
// if (debug_endofvariables!=0xaaaaaaaa) set_display(4); //DEBUG!!!
{
unsigned long autowritecounter;
autowritecounter_reset:
autowritecounter=0;
while( get_cmd_H() ) //dokud je Atari signal command na H
{
//tak bude zkoumat klavesnici
//buttons
unsigned char actual_key;
//read key bit
//PD7 .. RIGHT 0x80
//PD6 .. LEFT 0x40
//PD5 .. BOOT 0x20
//PD4
//PD3 .. WRITE ENABLE 0x08
//PD2 .. CARD INSERTED 0x04
//PD1
//PD0
actual_key = read_key(); //(inb(PIND)&0xe4)
if(actual_key != last_key)
{
if (inb(PIND)&0x04) goto SD_CARD_EJECTED;
//nyni je bit 0x04 vzdy 0 , protoze karta je vlozena
switch(actual_key) //&0xfe)
{
case 0xc0: //0x20^0xe0: //BOOT KEY
//actual_drive_number=0;
goto SET_BOOT_DRIVE;
break;
case 0xa0: //0x40^0xe0: //LEFT
if(actual_drive_number>1)
actual_drive_number--;
else
actual_drive_number=4;
break;
case 0x60: //0x80^0xe0: //RIGHT
if(actual_drive_number<4)
actual_drive_number++;
else
actual_drive_number=1;
break;
}
set_display(actual_drive_number);
last_key = actual_key;
Delay100usX(500); //pauza 50ms = 0.05s (kvuli tlacitkum)
}
if (autowritecounter>1700000)
{
// 100'000 nebylo temer poznat
//1'000'000 odpovida cca 1.75 sekundam
//2'000'000 odpovida cca 3.5 sekundam
//=>1s = 571428
//1'700'000 odpovida cca 3 sekundam
mmcWriteCachedFlush(); //pokud ceka nejaky sektor na zapis, zapise ho
//goto autowritecounter_reset;
autowritecounter=0;
}
else
autowritecounter++;
} //while
}
//Nacitani command frame
{
u08 err;
err=USART_Get_Buffer_And_Check(command,4,CMD_STATE_L);
//pokud je duvodem zmena cmd na H, nemusi cekat na wait_cmd_LH()
//if (err&0x01) goto change_sio_speed; //zvedl se cmd na H, ihned jde menit rychlost
Delay800us(); //t1 (650-950us) (Bez tehle pauzy to cele nefunguje!!!)
wait_cmd_LH(); //ceka az se zvedne signal command na H
Delay100us(); //T2=100 (po zvednuti command a pred ACKem)
if(err)
{
//bylo duvodem klepnuti do klavesnice?
if (err&0x02) goto ST_IDLE; //klepnul do klavesnice, takze nebude menit rychlost
if (fastsio_pokeydiv!=US_POKEY_DIV_STANDARD)
{
//prehodi ze standard na fast nebo naopak
fastsio_active=!fastsio_active;
}
change_sio_speed_by_fastsio_active:
{
u08 as;
as=ATARI_SPEED_STANDARD; //default speed
if (fastsio_active) as=fastsio_pokeydiv+6; //vzdy o 6 vic
USART_Init(as);
}
goto ST_IDLE;
}
}
if( command[0]>=0x31 && command[0]<(0x30+DEVICESNUM) ) //D1: az D4: (ano, az od D1: !!!)
{
// dostupne "z venci" je jen D1: az D4: (pres 0x31 az 0x34)
// ale pres zarizeni SDrive (0x71 az 0x74 podle zvoleneho cisla sdrive number)
// se da pristupovat vzdy primo k vD0:
// DISKETOVE OPERACE D0: az D4:
unsigned char virtual_drive_number;
if( command[0] == (unsigned char)0x31+3-((unsigned char)(inb(PINB))&0x03) )
{
//chce cist z drive Dx (D1: az D4:) ktere je prehazovaci
virtual_drive_number = actual_drive_number;
}
else
if ( command[0] == (unsigned char)0x30+actual_drive_number )
{
//chce cist z drive Dx (D0: az D4:) ktery je prehozen s drive number
virtual_drive_number = 4-((unsigned char)(inb(PINB))&0x03);
}
else
{
//chce cist z nektereho jineho Dx:
disk_operations_direct_d0_d4:
virtual_drive_number = command[0]&0xf;
}
if ( last_used_virtual_drive != virtual_drive_number )
{
//chce pracovat s jinym vDx nez se pracovalo posledne
unsigned char i;
unsigned char *p1;
unsigned char *p2;
if ( last_used_virtual_drive < DEVICESNUM )
{
p2=((unsigned char *)&FileInfo.vDisk);
p1=((unsigned char *)&vDisk[last_used_virtual_drive]);
// vDisk[last_used_virtual_drive] <- vDisk
//for(i=0; i<sizeof(virtual_disk_t); i++) p1[i]=p2[i];
//i=sizeof(virtual_disk_t); do { *p1++=*p2++; i--; } while(i);
for(i=0; i<sizeof(virtual_disk_t); i++) *p1++=*p2++;
}
p2=((unsigned char *)&FileInfo.vDisk);
p1=((unsigned char *)&vDisk[virtual_drive_number]);
// vDisk <- vDisk[virtual_drive_number]
//for(i=0; i<sizeof(virtual_disk_t); i++) p2[i]=p1[i];
//i=sizeof(virtual_disk_t); do { *p2++=*p1++; i--; } while(i);
for(i=0; i<sizeof(virtual_disk_t); i++) *p2++=*p1++;
//zmena last_used_virtual_drive
last_used_virtual_drive=virtual_drive_number; //zmena
}
if (!(FileInfo.vDisk.flags & FLAGS_DRIVEON))
{
//do daneho Dx neni vlozen disk
goto ST_IDLE;
}
//LED_GREEN_ON; // LED on
//pro cokoli jineho nez 0x53 (get status) nuluje FLAGS_WRITEERROR
if (command[1]!=0x53) FileInfo.vDisk.flags &= (~FLAGS_WRITEERROR); //vynuluje FLAGS_WRITEERROR bit
//formatovaci povely zpracovava v predsunu tady kvuli ACK/NACK
switch(command[1])
{
case 0x21: // format single + PERCOM
// Single for 810 and stock 1050 or as defined by CMD $4F for upgraded drives (Speedy, Happy, HDI, Black Box, XF 551).
//Returns 128 bytes (SD) or 256 bytes (DD) after format. Format ok requires that the first two bytes are $FF. It is said to return bad sector list (never seen one).
{ //zacatek sekce pres dva case, 0x21,0x22
u08 formaterror;
{
u32 singlesize;
singlesize = (u32)92160;
if ( !(FileInfo.vDisk.flags & FLAGS_XFDTYPE) ) singlesize+=(u32)16; //je to ATRko
if ( ( FileInfo.percomstate==3 ) //snazil se zapsat neodpovidajici percom
|| ( (FileInfo.percomstate==0) && (FileInfo.vDisk.size!=singlesize) ) //neni single disketa
)
{
goto Send_NACK_and_set_FLAGS_WRITEERROR_and_ST_IDLE;
}
}
{
unsigned short i,size;
if ( FileInfo.percomstate == 2)
{
size=256;
}
else
{
format_medium:
size=128;
}
FileInfo.percomstate=0; //po prvnim formatu pozbyva percom ucinnost
//XEX formatovat nelze
if (FileInfo.vDisk.flags & (FLAGS_XEXLOADER | FLAGS_READONLY))
{
goto Send_NACK_and_set_FLAGS_WRITEERROR_and_ST_IDLE;
}
//OK, bude se formatovat
send_ACK();
formaterror=0;
{
//vynuluje formatovany soubor
Clear_atari_sector_buffer_256();
{
unsigned long maz,psize;
maz=0;
if ( !(FileInfo.vDisk.flags & FLAGS_XFDTYPE) ) maz+=16;
while( (psize=(FileInfo.vDisk.size-maz))>0 )
{
if (psize>256) psize=256;
if (!faccess_offset(FILE_ACCESS_WRITE,maz,(u16)psize))
{
formaterror=1; //nepodaril se zapis
break;
}
maz+=psize;
}
}
}
//predchysta hodnoty 0xff nebo 0x00 v navratovem sektoru (co vraci povel format)
//a v pripade chyby nastavi FLAGS_WRITEERROR
{
u08 *ptr;
ptr=atari_sector_buffer;
i=size;
do { *ptr++=0xff; i--; } while(i>0);
if (formaterror)
{
atari_sector_buffer[0]=0x00; //err
atari_sector_buffer[1]=0x00; //err
FileInfo.vDisk.flags |= FLAGS_WRITEERROR; //nastavi FLAGS_WRITEERROR
}
}