forked from mist-devel/mist-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fpga.c
1024 lines (874 loc) · 25.5 KB
/
fpga.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
/*
Copyright 2005, 2006, 2007 Dennis van Weeren
Copyright 2008, 2009 Jakub Bednarski
This file is part of Minimig
Minimig is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Minimig is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// 2009-10-10 - any length (any multiple of 8 bytes) fpga core file support
// 2009-12-10 - changed command header id
// 2010-04-14 - changed command header id
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "errors.h"
#include "hardware.h"
#include "fdd.h"
#include "user_io.h"
#include "config.h"
#include "boot.h"
#include "osd.h"
#include "fpga.h"
#include "tos.h"
#include "mist_cfg.h"
#include "settings.h"
#include "usb/joymapping.h"
#ifndef DEFAULT_CORE_NAME
#define DEFAULT_CORE_NAME "CORE.RBF"
#endif
uint8_t rstval = 0;
#define CMD_HDRID 0xAACA
// TODO!
#define SPIN() asm volatile ( "mov r0, r0\n\t" \
"mov r0, r0\n\t" \
"mov r0, r0\n\t" \
"mov r0, r0");
extern char s[FF_LFN_BUF + 1];
extern adfTYPE df[4];
char minimig_ver_beta;
char minimig_ver_major;
char minimig_ver_minor;
char minimig_ver_minion;
char BootPrint(const char *text);
#ifdef XILINX_CCLK
// single byte serialization of FPGA configuration datastream
void ShiftFpga(unsigned char data)
{
AT91_REG *ppioa_codr = AT91C_PIOA_CODR;
AT91_REG *ppioa_sodr = AT91C_PIOA_SODR;
// bit 0
*ppioa_codr = XILINX_DIN | XILINX_CCLK;
if (data & 0x80)
*ppioa_sodr = XILINX_DIN;
*ppioa_sodr = XILINX_CCLK;
// bit 1
*ppioa_codr = XILINX_DIN | XILINX_CCLK;
if (data & 0x40)
*ppioa_sodr = XILINX_DIN;
*ppioa_sodr = XILINX_CCLK;
// bit 2
*ppioa_codr = XILINX_DIN | XILINX_CCLK;
if (data & 0x20)
*ppioa_sodr = XILINX_DIN;
*ppioa_sodr = XILINX_CCLK;
// bit 3
*ppioa_codr = XILINX_DIN | XILINX_CCLK;
if (data & 0x10)
*ppioa_sodr = XILINX_DIN;
*ppioa_sodr = XILINX_CCLK;
// bit 4
*ppioa_codr = XILINX_DIN | XILINX_CCLK;
if (data & 0x08)
*ppioa_sodr = XILINX_DIN;
*ppioa_sodr = XILINX_CCLK;
// bit 5
*ppioa_codr = XILINX_DIN | XILINX_CCLK;
if (data & 0x04)
*ppioa_sodr = XILINX_DIN;
*ppioa_sodr = XILINX_CCLK;
// bit 6
*ppioa_codr = XILINX_DIN | XILINX_CCLK;
if (data & 0x02)
*ppioa_sodr = XILINX_DIN;
*ppioa_sodr = XILINX_CCLK;
// bit 7
*ppioa_codr = XILINX_DIN | XILINX_CCLK;
if (data & 0x01)
*ppioa_sodr = XILINX_DIN;
*ppioa_sodr = XILINX_CCLK;
}
// Xilinx FPGA configuration
// was before unsigned char ConfigureFpga(void)
unsigned char ConfigureFpga(const char *name)
{
unsigned long t;
unsigned long n;
unsigned char *ptr;
FIL file;
UINT br;
// set outputs
*AT91C_PIOA_SODR = XILINX_CCLK | XILINX_DIN | XILINX_PROG_B;
// enable outputs
*AT91C_PIOA_OER = XILINX_CCLK | XILINX_DIN | XILINX_PROG_B;
// reset FGPA configuration sequence
// specs: PROG_B pulse min 0.3 us
t = 15;
while (--t)
*AT91C_PIOA_CODR = XILINX_PROG_B;
*AT91C_PIOA_SODR = XILINX_PROG_B;
// now wait for INIT to go high
// specs: max 2ms
t = 100000;
while (!(*AT91C_PIOA_PDSR & XILINX_INIT_B))
{
if (--t == 0)
{
iprintf("FPGA init is NOT high!\r");
FatalError(3);
}
}
iprintf("FPGA init is high\r");
if (*AT91C_PIOA_PDSR & XILINX_DONE)
{
iprintf("FPGA done is high before configuration!\r");
FatalError(3);
}
if(!name)
// name = "CORE.BIN";
name = "X7A102T.BIN";
// open bitstream file
if (f_open(&file, name, FA_READ) != FR_OK)
{
iprintf("No FPGA configuration file found!\r");
FatalError(4);
}
iprintf("FPGA bitstream file %s opened, file size = %llu\r", name, f_size(&file));
iprintf("[");
// send all bytes to FPGA in loop
t = 0;
n = f_size(&file) >> 3;
ptr = sector_buffer;
do
{
// read sector if 512 (64*8) bytes done
if ((t & 0x3F) == 0)
{
if (t & (1<<10))
DISKLED_OFF
else
DISKLED_ON
if ((t & 0x1FF) == 0)
iprintf("*");
if (f_read(&file, sector_buffer, 512, &br) != FR_OK) {
f_close(&file);
return(0);
}
ptr = sector_buffer;
}
// send data in packets of 8 bytes
ShiftFpga(*ptr++);
ShiftFpga(*ptr++);
ShiftFpga(*ptr++);
ShiftFpga(*ptr++);
ShiftFpga(*ptr++);
ShiftFpga(*ptr++);
ShiftFpga(*ptr++);
ShiftFpga(*ptr++);
t++;
}
while (t < n);
f_close(&file);
// return outputs to a state suitable for user_io.c
*AT91C_PIOA_SODR = XILINX_CCLK | XILINX_DIN | XILINX_PROG_B;
iprintf("]\r");
iprintf("FPGA bitstream loaded\r");
DISKLED_OFF;
// check if DONE is high
if (*AT91C_PIOA_PDSR & XILINX_DONE)
return(1);
iprintf("FPGA done is NOT high!\r");
FatalError(5);
return 0;
}
#endif
#ifdef ALTERA_DCLK
static inline void ShiftFpga(unsigned char data)
{
unsigned char i;
for ( i = 0; i < 8; i++ )
{
/* Dump to DATA0 and insert a positive edge pulse at the same time */
ALTERA_DATA0_RESET;
ALTERA_DCLK_RESET;
if(data & 1) ALTERA_DATA0_SET;
ALTERA_DCLK_SET;
data >>= 1;
}
}
// Altera FPGA configuration
unsigned char ConfigureFpga(const char *name)
{
unsigned long i;
unsigned char *ptr;
FIL file;
UINT br;
// set outputs
ALTERA_DCLK_SET;
ALTERA_NCONFIG_SET;
ALTERA_DATA0_SET;
if(!name)
name = DEFAULT_CORE_NAME;
// open bitstream file
if (f_open(&file, name, FA_READ) != FR_OK)
{
iprintf("No FPGA configuration file found!\r");
return ERROR_BITSTREAM_OPEN;
}
iprintf("FPGA bitstream file %s opened, file size = %llu\r", name, f_size(&file));
iprintf("[");
// send all bytes to FPGA in loop
ptr = sector_buffer;
ALTERA_START_CONFIG
/* Drive a transition of 0 to 1 to NCONFIG to indicate start of configuration */
for(i=0;i<10;i++)
ALTERA_NCONFIG_RESET; // must be low for at least 500ns
ALTERA_NCONFIG_SET;
// now wait for NSTATUS to go high
// specs: max 800us
i = 1000000;
while (!ALTERA_NSTATUS_STATE)
{
if (--i == 0)
{
ALTERA_STOP_CONFIG
iprintf("FPGA NSTATUS is NOT high!\r");
f_close(&file);
return ERROR_UPDATE_INIT_FAILED;
}
}
DISKLED_ON;
int t = 0;
int n = f_size(&file) >> 3;
/* Loop through every single byte */
for ( i = 0; i < f_size(&file); )
{
// read sector if SECTOR_BUFFER_SIZE bytes done
if ((i & (SECTOR_BUFFER_SIZE-1)) == 0)
{
if (i & (1<<13))
DISKLED_OFF
else
DISKLED_ON
if ((i & (SECTOR_BUFFER_SIZE*4-1)) == 0)
iprintf("*");
if (f_read(&file, sector_buffer, SECTOR_BUFFER_SIZE, &br) != FR_OK) {
f_close(&file);
return ERROR_READ_BITSTREAM_FAILED;
}
ptr = sector_buffer;
}
int bytes2copy = (i < f_size(&file) - 8)?8:f_size(&file)-i;
i += bytes2copy;
while(bytes2copy) {
ShiftFpga(*ptr++);
bytes2copy--;
}
/* Check for error through NSTATUS for every 10KB programmed and the last byte */
if ( !(i % 10240) || (i == f_size(&file) - 1) ) {
if ( !ALTERA_NSTATUS_STATE ) {
ALTERA_STOP_CONFIG
iprintf("FPGA NSTATUS is NOT high!\r");
f_close(&file);
return ERROR_UPDATE_PROGRESS_FAILED;
}
}
}
ALTERA_STOP_CONFIG
f_close(&file);
iprintf("]\r");
iprintf("FPGA bitstream loaded\r");
DISKLED_OFF;
// check if DONE is high
if (!ALTERA_DONE_STATE) {
iprintf("FPGA Configuration done but contains error... CONF_DONE is LOW\r");
return ERROR_UPDATE_FAILED;
}
/* Start initialization */
/* Clock another extra DCLK cycles while initialization is in progress
through internal oscillator or driving clock cycles into CLKUSR pin */
/* These extra DCLK cycles do not initialize the device into USER MODE */
/* It is not required to drive extra DCLK cycles at the end of configuration */
/* The purpose of driving extra DCLK cycles here is to insert some delay
while waiting for the initialization of the device to complete before
checking the CONFDONE and NSTATUS signals at the end of whole
configuration cycle */
for ( i = 0; i < 50; i++ )
{
ALTERA_DCLK_RESET;
ALTERA_DCLK_SET;
}
/* Initialization end */
if ( !ALTERA_NSTATUS_STATE || !ALTERA_DONE_STATE ) {
iprintf("FPGA Initialization finish but contains error: NSTATUS is %s and CONF_DONE is %s.\r",
ALTERA_NSTATUS_STATE?"HIGH":"LOW", ALTERA_DONE_STATE?"HIGH":"LOW" );
return ERROR_UPDATE_FAILED;
}
return ERROR_NONE;
}
#endif
void SendFile(FIL *file)
{
unsigned char c1, c2;
unsigned long j;
unsigned long n;
unsigned char *p;
iprintf("[");
n = (f_size(file) + 511) >> 9; // sector count (rounded up)
while (n--)
{
// read data sector from memory card
FileReadBlock(file,sector_buffer);
do
{
// read FPGA status
EnableFpga();
c1 = SPI(0);
c2 = SPI(0);
SPI(0);
SPI(0);
SPI(0);
SPI(0);
DisableFpga();
}
while (!(c1 & CMD_RDTRK));
if ((n & 15) == 0)
iprintf("*");
// send data sector to FPGA
EnableFpga();
c1 = SPI(0);
c2 = SPI(0);
SPI(0);
SPI(0);
SPI(0);
SPI(0);
p = sector_buffer;
for (j = 0; j < 512; j++)
SPI(*p++);
DisableFpga();
}
iprintf("]\r");
}
void SendFileEncrypted(FIL *file,unsigned char *key,int keysize)
{
UINT br;
unsigned char c1, c2;
unsigned char headersize;
unsigned int keyidx=0;
unsigned long j;
unsigned long n;
unsigned char *p;
int badbyte=0;
iprintf("[");
headersize=f_size(file)&255; // ROM should be a round number of kilobytes; overspill will likely be the Amiga Forever header.
f_read(file,sector_buffer,headersize, &br); // Read extra bytes
n = (f_size(file) + (511-headersize)) >> 9; // sector count (rounded up)
while (n--)
{
FileReadBlock(file,sector_buffer);
for (j = 0; j < 512; j++)
{
sector_buffer[j]^=key[keyidx++];
if(keyidx>=keysize)
keyidx-=keysize;
}
do
{
// read FPGA status
EnableFpga();
c1 = SPI(0);
c2 = SPI(0);
SPI(0);
SPI(0);
SPI(0);
SPI(0);
DisableFpga();
}
while (!(c1 & CMD_RDTRK));
if ((n & 15) == 0)
iprintf("*");
// send data sector to FPGA
EnableFpga();
c1 = SPI(0);
c2 = SPI(0);
SPI(0);
SPI(0);
SPI(0);
SPI(0);
p = sector_buffer;
for (j = 0; j < 512; j++)
SPI(*p++);
DisableFpga();
}
iprintf("]\r");
}
char kick1xfoundstr[] = "Kickstart v1.x found\n";
const char applymemdetectionpatchstr[] = "Applying Kickstart 1.x memory detection patch\n";
const char *kickfoundstr = NULL, *applypatchstr = NULL;
void PatchKick1xMemoryDetection() {
int applypatch = 0;
if (!strncmp(sector_buffer + 0x18, "exec 33.192 (8 Oct 1986)", 24)) {
kick1xfoundstr[13] = '2';
kickfoundstr = kick1xfoundstr;
goto applypatch;
}
if (!strncmp(sector_buffer + 0x18, "exec 34.2 (28 Oct 1987)", 23)) {
kick1xfoundstr[13] = '3';
kickfoundstr = kick1xfoundstr;
goto applypatch;
}
goto out;
applypatch:
if ((sector_buffer[0x154] == 0x66) && (sector_buffer[0x155] == 0x78)) {
applypatchstr = applymemdetectionpatchstr;
sector_buffer[0x154] = 0x60;
}
out:
return;
}
// SendFileV2 (for minimig_v2)
void SendFileV2(FIL* file, unsigned char* key, int keysize, int address, int size)
{
UINT br;
int i,j;
unsigned int keyidx=0;
iprintf("File size: %dkB\r", size>>1);
iprintf("[");
if (keysize) {
// read header
f_read(file, sector_buffer, 0xb, &br);
}
for (i=0; i<size; i++) {
if (!(i&31)) iprintf("*");
FileReadBlock(file, sector_buffer);
if (keysize) {
// decrypt ROM
for (j=0; j<512; j++) {
sector_buffer[j] ^= key[keyidx++];
if(keyidx >= keysize) keyidx -= keysize;
}
}
// patch kickstart 1.x to force memory detection every time the AMIGA is reset
if (minimig_cfg.kick1x_memory_detection_patch && (i == 0 || i == 512)) {
kickfoundstr = NULL;
applypatchstr = NULL;
PatchKick1xMemoryDetection();
}
EnableOsd();
unsigned int adr = address + i*512;
SPI(OSD_CMD_WR);
SPIN(); SPIN(); SPIN(); SPIN();
SPI(adr&0xff); adr = adr>>8;
SPI(adr&0xff); adr = adr>>8;
SPIN(); SPIN(); SPIN(); SPIN();
SPI(adr&0xff); adr = adr>>8;
SPI(adr&0xff); adr = adr>>8;
SPIN(); SPIN(); SPIN(); SPIN();
for (j=0; j<512; j=j+4) {
SPI(sector_buffer[j+0]);
SPI(sector_buffer[j+1]);
SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN();
SPI(sector_buffer[j+2]);
SPI(sector_buffer[j+3]);
SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN();
}
DisableOsd();
}
iprintf("]\r");
if (kickfoundstr) {
iprintf(kickfoundstr);
}
if (applypatchstr) {
iprintf(applypatchstr);
}
}
// draw on screen
char BootDraw(char *data, unsigned short len, unsigned short offset)
{
DEBUG_FUNC_IN();
unsigned char c1, c2, c3, c4;
unsigned char cmd;
const char *p;
unsigned short n;
unsigned short i;
n = (len+3)&(~3);
i = 0;
cmd = 1;
while (1)
{
EnableFpga();
c1 = SPI(0x10); // track read command
c2 = SPI(0x01); // disk present
unsigned char x = SPI(0);
unsigned char y = SPI(0);
c3 = SPI(0);
c4 = SPI(0);
// iprintf("FPGA state: %d %d (%d %d) %d %d\n", c1, c2, x, y, c3, c4);
if (c1 & CMD_RDTRK)
{
if (cmd)
{ // command phase
if (c3 == 0x80 && c4 == 0x06) // command packet size must be 12 bytes
{
cmd = 0;
SPI(CMD_HDRID >> 8); // command header
SPI(CMD_HDRID & 0xFF);
SPI(0x00); // cmd: 0x0001 = print text
SPI(0x01);
// data packet size in bytes
SPI(0x00);
SPI(0x00);
SPI((n)>>8);
SPI((n)&0xff); // +2 because only even byte count is possible to send and we have to send termination zero byte
// offset
SPI(0x00);
SPI(0x00);
SPI(offset>>8);
SPI(offset&0xff);
}
else
break;
}
else
{ // data phase
if (c3 == 0x80 && c4 == ((n) >> 1))
{
p = data;
n = c4 << 1;
while (n--)
{
c4 = *p;
SPI((i>=len) ? 0 : c4);
p++;
i++;
}
DisableFpga();
return 1;
}
else
break;
}
}
DisableFpga();
}
DisableFpga();
return 0;
DEBUG_FUNC_OUT();
}
// print message on the boot screen
char BootPrint(const char *text)
{
if(!minimig_v1()) {
iprintf("%s\n", text);
return 0;
}
unsigned char c1, c2, c3, c4;
unsigned char cmd;
const char *p;
unsigned char n;
return 0;
p = text;
n = 0;
while (*p++ != 0)
n++; // calculating string length
cmd = 1;
while (1)
{
EnableFpga();
c1 = SPI(0x10); // track read command
c2 = SPI(0x01); // disk present
SPI(0);
SPI(0);
c3 = SPI(0);
c4 = SPI(0);
if (c1 & CMD_RDTRK)
{
if (cmd)
{ // command phase
if (c3 == 0x80 && c4 == 0x06) // command packet size must be 12 bytes
{
cmd = 0;
SPI(CMD_HDRID >> 8); // command header
SPI(CMD_HDRID & 0xFF);
SPI(0x00); // cmd: 0x0001 = print text
SPI(0x01);
// data packet size in bytes
SPI(0x00);
SPI(0x00);
SPI(0x00);
SPI(n+2); // +2 because only even byte count is possible to send and we have to send termination zero byte
// don't care
SPI(0x00);
SPI(0x00);
SPI(0x00);
SPI(0x00);
}
else
break;
}
else
{ // data phase
if (c3 == 0x80 && c4 == ((n + 2) >> 1))
{
p = text;
n = c4 << 1;
while (n--)
{
c4 = *p;
SPI(c4);
if (c4) // if current character is not zero go to next one
p++;
}
DisableFpga();
return 1;
}
else
break;
}
}
DisableFpga();
}
DisableFpga();
return 0;
}
char PrepareBootUpload(unsigned char base, unsigned char size)
// this function sends given file to Minimig's memory
// base - memory base address (bits 23..16)
// size - memory size (bits 23..16)
{
unsigned char c1, c2, c3, c4;
unsigned char cmd = 1;
while (1)
{
EnableFpga();
c1 = SPI(0x10); // track read command
c2 = SPI(0x01); // disk present
SPI(0);
SPI(0);
c3 = SPI(0);
c4 = SPI(0);
if (c1 & CMD_RDTRK)
{
if (cmd)
{ // command phase
if (c3 == 0x80 && c4 == 0x06) // command packet size 12 bytes
{
cmd = 0;
SPI(CMD_HDRID >> 8); // command header
SPI(CMD_HDRID & 0xFF);
SPI(0x00);
SPI(0x02); // cmd: 0x0002 = upload memory
// memory base address
SPI(0x00);
SPI(base);
SPI(0x00);
SPI(0x00);
// memory size
SPI(0x00);
SPI(size);
SPI(0x00);
SPI(0x00);
}
else
break;
}
else
{ // data phase
DisableFpga();
iprintf("Ready to upload ROM file...\r");
// send rom image to FPGA
// SendFile(file);
// iprintf("ROM file uploaded.\r");
return 0;
}
}
DisableFpga();
}
DisableFpga();
return -1;
}
void BootExit(void)
{
unsigned char c1, c2, c3, c4;
while (1)
{
EnableFpga();
c1 = SPI(0x10); // track read command
c2 = SPI(0x01); // disk present
SPI(0);
SPI(0);
c3 = SPI(0);
c4 = SPI(0);
if (c1 & CMD_RDTRK)
{
if (c3 == 0x80 && c4 == 0x06) // command packet size 12 bytes
{
SPI(CMD_HDRID >> 8); // command header
SPI(CMD_HDRID & 0xFF);
SPI(0x00); // cmd: 0x0003 = restart
SPI(0x03);
// don't care
SPI(0x00);
SPI(0x00);
SPI(0x00);
SPI(0x00);
// don't care
SPI(0x00);
SPI(0x00);
SPI(0x00);
SPI(0x00);
}
DisableFpga();
return;
}
DisableFpga();
}
}
void ClearMemory(unsigned long base, unsigned long size)
{
unsigned char c1, c2, c3, c4;
while (1)
{
EnableFpga();
c1 = SPI(0x10); // track read command
c2 = SPI(0x01); // disk present
SPI(0);
SPI(0);
c3 = SPI(0);
c4 = SPI(0);
if (c1 & CMD_RDTRK)
{
if (c3 == 0x80 && c4 == 0x06)// command packet size 12 bytes
{
SPI(CMD_HDRID >> 8); // command header
SPI(CMD_HDRID & 0xFF);
SPI(0x00); // cmd: 0x0004 = clear memory
SPI(0x04);
// memory base
SPI((unsigned char)(base >> 24));
SPI((unsigned char)(base >> 16));
SPI((unsigned char)(base >> 8));
SPI((unsigned char)base);
// memory size
SPI((unsigned char)(size >> 24));
SPI((unsigned char)(size >> 16));
SPI((unsigned char)(size >> 8));
SPI((unsigned char)size);
}
DisableFpga();
return;
}
DisableFpga();
}
}
unsigned char GetFPGAStatus(void)
{
unsigned char status;
EnableFpga();
status = SPI(0);
SPI(0);
SPI(0);
SPI(0);
SPI(0);
SPI(0);
DisableFpga();
return status;
}
unsigned char fpga_init(const char *name) {
unsigned long time = GetRTTC();
int loaded_from_usb = USB_LOAD_VAR;
unsigned char ct;
// load the global MISTCFG.INI here
// loading between the FPGA init and detect_core_type breaks with some SD-Cards. Reason unknown.
virtual_joystick_remap_init(false);
settings_load(true);
iprintf("loaded_from_usb = %d\n", USB_LOAD_VAR == USB_LOAD_VALUE);
USB_LOAD_VAR = 0;
if((loaded_from_usb != USB_LOAD_VALUE) && !user_io_dip_switch1()) {
unsigned char err = ConfigureFpga(name);
if (err != ERROR_NONE) return err;
time = GetRTTC() - time;
iprintf("FPGA configured in %lu ms\r", time);
}
// wait max 100 msec for a valid core type
time = GetTimer(100);
do {
EnableIO();
ct = SPI(0xff);
DisableIO();
SPI_MINIMIGV1_HACK
} while( ((ct == 0) || (ct == 0xff)) && !CheckTimer(time));
iprintf("ident = %x\n", ct);
user_io_detect_core_type();
user_io_init_core();
mist_ini_parse();
user_io_send_buttons(true);
InitDB9();
if((user_io_core_type() == CORE_TYPE_MINIMIG)||
(user_io_core_type() == CORE_TYPE_MINIMIG2)) {
puts("Running minimig setup");
if(minimig_v2()) {
user_io_8bit_set_status(minimig_cfg.clock_freq << 1, 0xffffffff);
WaitTimer(100);
EnableOsd();
SPI(OSD_CMD_VERSION);
minimig_ver_beta = SPI(0xff);
minimig_ver_major = SPI(0xff);
minimig_ver_minor = SPI(0xff);
minimig_ver_minion = SPI(0xff);
DisableOsd();
SPIN(); SPIN(); SPIN(); SPIN();
EnableOsd();
SPI(OSD_CMD_RST);
rstval = (SPI_RST_USR | SPI_RST_CPU | SPI_CPU_HLT);
SPI(rstval);
DisableOsd();
SPIN(); SPIN(); SPIN(); SPIN();
EnableOsd();
SPI(OSD_CMD_RST);
rstval = (SPI_RST_CPU | SPI_CPU_HLT);
SPI(rstval);
DisableOsd();
SPIN(); SPIN(); SPIN(); SPIN();
WaitTimer(100);
BootInit();
WaitTimer(500);
char rtl_ver[45];
siprintf(rtl_ver, "**** MINIMIG-AGA%s v%d.%d.%d for MiST ****", minimig_ver_beta ? " BETA" : "", minimig_ver_major, minimig_ver_minor, minimig_ver_minion);
BootPrintEx(rtl_ver);
BootPrintEx(" ");
BootPrintEx("MINIMIG-AGA for MiST by Rok Krajnc ([email protected])");
BootPrintEx("Original Minimig by Dennis van Weeren");
BootPrintEx("Updates by Jakub Bednarski, Tobias Gubener, Sascha Boing, A.M. Robinson & others");
BootPrintEx("MiST by Till Harbaum ([email protected])");
BootPrintEx("For updates & code see https://github.com/rkrajnc/minimig-mist");
BootPrintEx(" ");
WaitTimer(1000);
}
ChangeDirectoryName("/");
//eject all disk