forked from meesokim/spc1000
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpioex.c
802 lines (712 loc) · 24.8 KB
/
gpioex.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
//
// How to access GPIO registers from C-code on the Raspberry-Pi
// Example program
// 15-January-2012
// Dom and Gert
// Revised: 15-Feb-2013
// Access from ARM Running Linux
#define BCM2708_PERI_BASE 0x20000000
#define BCM2709_PERI_BASE 0x3f000000
#define GPIO_BASE_OFFSET 0x200000
#define SETUP_OK 0
#define SETUP_DEVMEM_FAIL 1
#define SETUP_MALLOC_FAIL 2
#define SETUP_MMAP_FAIL 3
#define SETUP_CPUINFO_FAIL 4
#define SETUP_NOT_RPI_FAIL 5
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (4*1024)
#define FSEL_OFFSET 0 // 0x0000
#define SET_OFFSET 7 // 0x001c / 4
#define CLR_OFFSET 10 // 0x0028 / 4
#define PINLEVEL_OFFSET 13 // 0x0034 / 4
#define EVENT_DETECT_OFFSET 16 // 0x0040 / 4
#define RISING_ED_OFFSET 19 // 0x004c / 4
#define FALLING_ED_OFFSET 22 // 0x0058 / 4
#define HIGH_DETECT_OFFSET 25 // 0x0064 / 4
#define LOW_DETECT_OFFSET 28 // 0x0070 / 4
#define PULLUPDN_OFFSET 37 // 0x0094 / 4
#define PULLUPDNCLK_OFFSET 38 // 0x0098 / 4
#define INT 2
#define NMI 3
#define CLK 4
#define HALT 17
#define DB7 27
#define DB2 22
#define MREQ 5
#define IORQ 6
#define RD 13
#define WR 19
#define DB6 26
#define DB1 21
#define DB0 20
#define WAIT 12
#define RESET 7
#define CS_16 8
#define DB5 25
#define DB4 24
#define DB3 23
#define BUSRQ 18
#define REFSH 14
#define M1 15
#define BUSAK 16
#define SI 10
#define SO 9
#define CP 11
#define M 0
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
typedef char byte;
typedef char bool;
#define false 0
#define true 1
#define SPI_CHAN 0
#define NUM_TIMES 100
#define MAX_SIZE (1024*1024)
int mem_fd;
void *gpio_map;
// I/O access
volatile unsigned *gpio;
int halt;
int mreq;
int iorq;
int rfsh;
int m1;
int busak;
int wr;
int rd;
// Control *input* pins of Z80, we write them into the dongle
int zint = 1;
int nmi = 1;
int reset = 1;
int busrq = 1;
int wait = 1;
// Content of address and data wires
unsigned short ab;
byte db;
// Clock counter after reset
int clkCount;
int clkCountHi;
// T-cycle counter
int T;
int Mlast;
// M1-cycle counter
int m1Count;
// Detection if the address or data bus is tri-stated
bool abTristated = false;
bool dbTristated = false;
// Simulation control variables
bool running = 1; // Simulation is running or is stopped
int traceShowBothPhases; // Show both phases of a clock cycle
int traceRefresh; // Trace refresh cycles
int tracePause; // Pause for a key press every so many clocks
int tracePauseCount; // Current clock count for tracePause
int stopAtClk; // Stop the simulation after this many clocks
int stopAtM1; // Stop at a specific M1 cycle number
int stopAtHalt; // Stop when HALT signal gets active
int intAtClk; // Issue INT signal at that clock number
int nmiAtClk; // Issue NMI signal at that clock number
int busrqAtClk; // Issue BUSRQ signal at that clock number
int resetAtClk; // Issue RESET signal at that clock number
int waitAtClk; // Issue WAIT signal at that clock number
int clearAtClk; // Clear all control signals at that clock number
byte iorqVector; // Push IORQ vector (default is FF)
// Buffer containing RAM memory for Z80 to access
byte ram[256*256];
#define TEMP_SIZE 512
char temp[TEMP_SIZE];
static int myFd ;
#if 0
// GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
#define GPIO_SET(s) *(gpio+SET_OFFSET) = 1<<s // sets bits which are 1 ignores bits which are 0
#define GPIO_CLR(c) *(gpio+CLR_OFFSET) = 1<<c // clears bits which are 1 ignores bits which are 0
#define GET_GPIO(g) ((*(gpio+PINLEVEL_OFFSET))&(1<<g)) // 0 if LOW, (1<<g) if HIGH
#define GPIO_PULL *(gpio+PULLUPDN_OFFSET) // Pull up/pull down
#define GPIO_PULLCLK0 *(gpio+PULLUPDNCLK_OFFSET) // Pull up/pull down clock
inline void digitalWrite(int g, int val)
{
if (val == LOW)
GPIO_CLR(g);
else
GPIO_SET(g);
}
inline void pinMode(int g, int dir)
{
if (dir == OUTPUT)
OUT_GPIO(g);
else
INP_GPIO(g);
}
inline int digitalRead(int g)
{
return GET_GPIO(g);
}
#else
#define INP_GPIO(g) pinMode(g, INPUT)
#define OUT_GPIO(g) pinMode(g, OUTPUT)
#define GPIO_SET(g) digitalWrite(g, HIGH)
#define GPIO_CLR(g) digitalWrite(g, LOW)
#define GET_GPIO(g) digitalRead(g)
#endif
#include <time.h>
int msleep(unsigned long milisec)
{
struct timespec req={0};
time_t sec=(int)(milisec/1000);
milisec=milisec-(sec*1000);
req.tv_sec=sec;
req.tv_nsec=milisec*1000000L;
while(nanosleep(&req,&req)==-1)
continue;
return 1;
}
void spiSetup (int speed)
{
if ((myFd = wiringPiSPISetup (SPI_CHAN, speed)) < 0)
{
fprintf (stderr, "Can't open the SPI bus: %s\n", strerror (errno)) ;
exit (EXIT_FAILURE) ;
}
}
// Read and return one ASCII hex value from a string
byte hex(char *s){
byte nibbleH = (*s - '0') & ~(1<<5);
byte nibbleL = (*(s+1) - '0') & ~(1<<5);
if (nibbleH>9) nibbleH -= 7;
if (nibbleL>9) nibbleL -= 7;
return (nibbleH << 4) | nibbleL;
}
// Read and return one ASCII hex value from a temp buffer given the index
// of that hex number. This is used only to read Intel HEX format buffer.
byte hexFromTemprintf(char *pTemp, int index)
{
int start = (index*2)+1;
return hex(pTemp + start);
}
#define OUTPUT 1
#define INPUT 0
#define HIGH 1
#define LOW 0
#define delay msleep
int setup_io();
void loop();
char extraInfo[64] = { "" };
void printButton(int g)
{
if (GET_GPIO(g)) // !=0 <-> bit is 1 <- port is HIGH=3.3V
printf("Button pressed!\n");
else // port is LOW=0V
printf("Button released!\n");
}
// Resets all simulation variables to their defaults
void ResetSimulationVars()
{
traceShowBothPhases = 0;// Show both phases of a clock cycle
traceRefresh = 0; // Trace refresh cycles
tracePause = -1; // Pause for a keypress every so many clocks
stopAtClk = -1; // Stop the simulation after this many clocks
stopAtM1 = -1; // Stop at a specific M1 cycle number
stopAtHalt = 1; // Stop when HALT signal gets active
intAtClk = -1; // Issue INT signal at that clock number
nmiAtClk = -1; // Issue NMI signal at that clock number
busrqAtClk = -1; // Issue BUSRQ signal at that clock number
resetAtClk = -1; // Issue RESET signal at that clock number
waitAtClk = -1; // Issue WAIT signal at that clock number
clearAtClk = -1; // Clear all control signals at that clock number
iorqVector = 0xFF; // Push IORQ vector
}
// Issue a RESET sequence to Z80 and reset internal counters
void DoReset()
{
int i;
printf("\r\n:Starting the clock\r\n");
digitalWrite(RESET, LOW); delay(1);
// Reset should be kept low for 3 full clock cycles
for( i=0; i<3; i++)
{
digitalWrite(CLK, HIGH); delay(1);
digitalWrite(CLK, LOW); delay(1);
}
printf(":Releasing RESET\r\n");
digitalWrite(RESET, HIGH); delay(1);
// Do not count initial 2 clocks after the reset
clkCount = -2;
T = 0;
Mlast = 1;
tracePauseCount = 0;
m1Count = 0;
}
// Write all control pins into the Z80 dongle
void WriteControlPins()
{
digitalWrite(INT, zint ? HIGH : LOW);
digitalWrite(NMI, nmi ? HIGH : LOW);
digitalWrite(RESET, reset ? HIGH : LOW);
digitalWrite(BUSRQ, busrq ? HIGH : LOW);
digitalWrite(WAIT, wait ? HIGH : LOW);
}
// Set new data value into the Z80 data bus
void SetDataToDB(byte data)
{
pinMode(DB0, OUTPUT);
pinMode(DB1, OUTPUT);
pinMode(DB2, OUTPUT);
pinMode(DB3, OUTPUT);
pinMode(DB4, OUTPUT);
pinMode(DB5, OUTPUT);
pinMode(DB6, OUTPUT);
pinMode(DB7, OUTPUT);
digitalWrite(DB0, (data & (1<<0)) ? HIGH : LOW);
digitalWrite(DB1, (data & (1<<1)) ? HIGH : LOW);
digitalWrite(DB2, (data & (1<<2)) ? HIGH : LOW);
digitalWrite(DB3, (data & (1<<3)) ? HIGH : LOW);
digitalWrite(DB4, (data & (1<<4)) ? HIGH : LOW);
digitalWrite(DB5, (data & (1<<5)) ? HIGH : LOW);
digitalWrite(DB6, (data & (1<<6)) ? HIGH : LOW);
digitalWrite(DB7, (data & (1<<7)) ? HIGH : LOW);
db = data;
dbTristated = false;
}
// Read Z80 data bus and store into db variable
void GetDataFromDB()
{
pinMode(DB0, INPUT);
pinMode(DB1, INPUT);
pinMode(DB2, INPUT);
pinMode(DB3, INPUT);
pinMode(DB4, INPUT);
pinMode(DB5, INPUT);
pinMode(DB6, INPUT);
pinMode(DB7, INPUT);
digitalWrite(DB0, LOW);
digitalWrite(DB1, LOW);
digitalWrite(DB2, LOW);
digitalWrite(DB3, LOW);
digitalWrite(DB4, LOW);
digitalWrite(DB5, LOW);
digitalWrite(DB6, LOW);
digitalWrite(DB7, LOW);
// Detect if the data bus is tri-stated
delay(1);
// int test0 = analogRead(DB0);
// These numbers might need to be adjusted for each Arduino board
//dbTristated = test0>HI_Z_LOW && test0<HI_Z_HIGH;
byte d0 = digitalRead(DB0);
byte d1 = digitalRead(DB1);
byte d2 = digitalRead(DB2);
byte d3 = digitalRead(DB3);
byte d4 = digitalRead(DB4);
byte d5 = digitalRead(DB5);
byte d6 = digitalRead(DB6);
byte d7 = digitalRead(DB7);
db = (d7<<7)|(d6<<6)|(d5<<5)|(d4<<4)|(d3<<3)|(d2<<2)|(d1<<1)|d0;
}
// Read a value of Z80 address bus and store it into the ab variable.
// In addition, try to detect when a bus is tri-stated and write 0xFFF if so.
void GetAddressFromAB()
{
int i;
int x = 0;
digitalWrite(M, HIGH);
digitalWrite(CS_16, LOW);
digitalWrite(CP, LOW);
digitalWrite(M, LOW);
for(i=0;i<16;i++)
{
digitalWrite(CP, LOW);
digitalWrite(CP, LOW);
digitalWrite(CP, LOW);
x= (x << 1) | digitalRead(SO);
digitalWrite(CP, HIGH);
}
digitalWrite(CP, LOW);
digitalWrite(CS_16, HIGH);
digitalWrite(CP, HIGH);
}
// Read all control pins on the Z80 and store them into internal variables
void ReadControlState()
{
halt = digitalRead(HALT);
mreq = digitalRead(MREQ);
iorq = digitalRead(IORQ);
rfsh = digitalRead(REFSH);
m1 = digitalRead(M1);
busak = digitalRead(BUSAK);
wr = digitalRead(WR);
rd = digitalRead(RD);
}
// Dump the Z80 state as stored in internal variables
void DumpState(bool suppress)
{
if (!suppress)
{
// Select your character for tri-stated bus
char abStr[4] = { "---" };
char dbStr[3] = { "--" };
if (!abTristated) sprintf(abStr, "%03X", ab);
if (!dbTristated) sprintf(dbStr, "%02X", db);
if (T==1 && clkCountHi)
printf("-----------------------------------------------------------+\r\n");
printf("#%03d%c T%-2d AB:%s DB:%s %s %s %s %s %s %s %s %s |%s%s%s%s %s\r\n",
clkCount<0? 0 : clkCount, clkCountHi ? 'H' : 'L', T,
abStr, dbStr,
m1?" ":"M1", rfsh?" ":"RFSH", mreq?" ":"MREQ", rd?" ":"RD", wr?" ":"WR", iorq?" ":"IORQ", busak?" ":"BUSAK",halt?" ":"HALT",
zint?"":"[INT]", nmi?"":"[NMI]", busrq?"":"[BUSRQ]", wait?"":"[WAIT]",
extraInfo);
}
extraInfo[0] = 0;
}
int main(int argc, char **argv)
{
int g,rep, i;
// Set up gpi pointer for direct register access
wiringPiSetupGpio ();
setup_io();
for (i = 0; i < 28; i++)
{
INP_GPIO(i);
printf("%d", GET_GPIO(i));
}
printf("\n");
//exit(0);
OUT_GPIO(INT); GPIO_SET(INT);
OUT_GPIO(NMI); GPIO_SET(NMI);
OUT_GPIO(CLK); GPIO_SET(CLK);
OUT_GPIO(WAIT); GPIO_SET(WAIT);
OUT_GPIO(BUSRQ); GPIO_SET(BUSRQ);
OUT_GPIO(RESET); GPIO_SET(RESET);
OUT_GPIO(SI); GPIO_SET(SI);
OUT_GPIO(CP); GPIO_SET(CP);
OUT_GPIO(CS_16); GPIO_SET(CS_16);
OUT_GPIO(M);
ResetSimulationVars();
WriteControlPins();
DoReset();
while(1)
loop();
}
void loop()
{
int i, j;
// delay(1); GPIO_SET(CLK); delay(1);
GPIO_SET(CLK);
clkCountHi = 1;
clkCount++;
T++;
tracePauseCount++;
ReadControlState();
GetAddressFromAB();
if (Mlast==1 && m1==0)
T = 1, m1Count++;
Mlast = m1;
bool suppressDump = false;
if (!traceRefresh & !rfsh) suppressDump = true;
// If the number of M1 cycles has been reached, skip the rest since we dont
// want to execute this M1 phase
if (m1Count==stopAtM1)
{
sprintf(extraInfo, "Number of M1 cycles reached"), running = false;
printf("-----------------------------------------------------------+\r\n");
goto control;
}
// If the address is tri-stated, skip checking various combinations of
// control signals since they may also be floating and we can't detect that
if (!abTristated)
{
// Simulate read from RAM
if (!mreq && !rd)
{
SetDataToDB(ram[ab & 0xFFFF]);
if (!m1)
sprintf(extraInfo, "Opcode read from %03X -> %02X", ab, ram[ab & 0xFF]);
else
sprintf(extraInfo, "Memory read from %03X -> %02X", ab, ram[ab & 0xFF]);
if (++ab % 0x1000 == 0)
printf("%04X\n",ab);
}
else
// Simulate interrupt requesting a vector
if (!m1 && !iorq)
{
SetDataToDB(iorqVector);
sprintf(extraInfo, "Pushing vector %02X", iorqVector);
}
else
GetDataFromDB();
// Simulate write to RAM
if (!mreq && !wr)
{
ram[ab & 0xFFFF] = db;
sprintf(extraInfo, "Memory write to %03X <- %02X", ab, db);
}
// Detect I/O read: We don't place anything on the bus
if (!iorq && !rd)
{
sprintf(extraInfo, "I/O read from %03X", ab);
}
// Detect I/O write
if (!iorq && !wr)
{
sprintf(extraInfo, "I/O write to %03X <- %02X", ab, db);
}
// Capture memory refresh cycle
if (!mreq && !rfsh)
{
sprintf(extraInfo, "Refresh address %03X", ab);
}
}
else
GetDataFromDB();
DumpState(suppressDump);
// If the user wanted to pause simulation after a certain number of
// clocks, handle it here. If the key pressed to continue was not Enter,
// stop the simulation to issue that command
if (tracePause==tracePauseCount)
{
tracePauseCount = 0;
}
//--------------------------------------------------------
// Clock goes low
//--------------------------------------------------------
//delay(1); digitalWrite(CLK, LOW); delay(1);
digitalWrite(CLK, LOW);
clkCountHi = 0;
if (traceShowBothPhases)
{
ReadControlState();
GetAddressFromAB();
DumpState(suppressDump);
}
// Perform various actions at the requested clock number
// if the count is positive (we start it at -2 to skip initial 2T)
if (clkCount>=0)
{
if (clkCount==intAtClk) zint = 0;
if (clkCount==nmiAtClk) nmi = 0;
if (clkCount==busrqAtClk) busrq = 0;
if (clkCount==resetAtClk) reset = 0;
if (clkCount==waitAtClk) wait = 0;
// De-assert all control pins at this clock number
if (clkCount==clearAtClk)
zint = nmi = busrq = reset = wait = 1;
WriteControlPins();
// Stop the simulation under some conditions
if (clkCount==stopAtClk)
sprintf(extraInfo, "Number of clocks reached"), running = false;
if (stopAtHalt&!halt)
sprintf(extraInfo, "HALT instruction"), running = false;
}
//--------------------------------------------------------
// Trace/simulation control handler
//--------------------------------------------------------
control:
if (!running)
{
printf(":Simulation stopped: %s\r\n", extraInfo);
extraInfo[0] = 0;
digitalWrite(CLK, HIGH);
zint = nmi = busrq = wait = 1;
WriteControlPins();
while(!running)
{
// Expect a command from the serial port
// if (Serial.available()>0)
{
memset((void *)temp, 0, TEMP_SIZE);
gets(temp);
//Serial.readBytesUntil('\r', temp, TEMP_SIZE-1);
// Option ":" : this is not really a user option. This is used to
// Intel HEX format values into the RAM buffer
// Multiple lines may be pasted. They are separated by a space character.
char *pTemp = temp;
while (*pTemp==':')
{
byte bytes = hexFromTemprintf(pTemp, 0);
if (bytes>0)
{
int address = (hexFromTemprintf(pTemp, 1)<<8) + hexFromTemprintf(pTemp, 2);
byte recordType = hexFromTemprintf(pTemp, 3);
printf("%04X:", address);
for (i=0; i<bytes; i++)
{
ram[(address + i) & 0xFF] = hexFromTemprintf(pTemp, 4+i);
printf(" %02X", hexFromTemprintf(pTemp, 4+i));
}
printf("\r\n");
}
pTemp += bytes*2 + 12; // Skip to the next possible line of hex entry
}
// Option "r" : reset and run the simulation
if (temp[0]=='r')
{
// If the variable 9 (Issue RESET) is not set, perform a RESET and run the simulation.
// If the variable was set, skip reset sequence since we might be testing it.
if (resetAtClk<0)
DoReset();
running = true;
}
// Option "sc" : clear simulation variables to their default values
if (temp[0]=='s' && temp[1]=='c')
{
ResetSimulationVars();
temp[1] = 0; // Proceed to dump all variables...
}
// Option "s" : show and set internal control variables
if (temp[0]=='s' && temp[1]!='c')
{
// Show or set the simulation parameters
int var = 0, value;
int args = sscanf(&temp[1], "%d %d\r\n", &var, &value);
// Parameter for the option #12 is read in as a hex; others are decimal by default
if (var==12)
args = sscanf(&temp[1], "%d %x\r\n", &var, &value);
if (args==2)
{
if (var==0) traceShowBothPhases = value;
if (var==1) traceRefresh = value;
if (var==2) tracePause = value;
if (var==3) stopAtClk = value;
if (var==4) stopAtM1 = value;
if (var==5) stopAtHalt = value;
if (var==6) intAtClk = value;
if (var==7) nmiAtClk = value;
if (var==8) busrqAtClk = value;
if (var==9) resetAtClk = value;
if (var==10) waitAtClk = value;
if (var==11) clearAtClk = value;
if (var==12) iorqVector = value & 0xFF;
}
printf("------ Simulation variables ------\r\n");
printf("#0 Trace both clock phases = %d\r\n", traceShowBothPhases);
printf("#1 Trace refresh cycles = %d\r\n", traceRefresh);
printf("#2 Pause for keypress every = %d\r\n", tracePause);
printf("#3 Stop after clock # = %d\r\n", stopAtClk);
printf("#4 Stop after # M1 cycles = %d\r\n", stopAtM1);
printf("#5 Stop at HALT = %d\r\n", stopAtHalt);
printf("#6 Issue INT at clock # = %d\r\n", intAtClk);
printf("#7 Issue NMI at clock # = %d\r\n", nmiAtClk);
printf("#8 Issue BUSRQ at clock # = %d\r\n", busrqAtClk);
printf("#9 Issue RESET at clock # = %d\r\n", resetAtClk);
printf("#10 Issue WAIT at clock # = %d\r\n", waitAtClk);
printf("#11 Clear all at clock # = %d\r\n", clearAtClk);
printf("#12 Push IORQ vector #(hex) = %2X\r\n", iorqVector);
}
// Option "m" : dump RAM memory
if (temp[0]=='m' && temp[1]!='c')
{
// Dump the content of a RAM buffer
printf(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n");
printf(" +-----------------------------------------------\r\n");
for(i=0; i<16; i++)
{
printf("%02X |", i);
for(j=0; j<16; j++)
{
printf("%02X ", ram[i*16+j]);
}
printf("\r\n");
}
}
// Option "mc" : clear RAM memory
if (temp[0]=='m' && temp[1]=='c')
{
memset(ram, 0, sizeof(ram));
printf("RAM cleared\r\n");
}
// Option "?" : print help
if (temp[0]=='?' || temp[0]=='h')
{
printf("s - show simulation variables\r\n");
printf("s #var value - set simulation variable number to a value\r\n");
printf("sc - clear simulation variables to their default values\r\n");
printf("r - restart the simulation\r\n");
printf(":INTEL-HEX - reload RAM buffer with a given data stream\r\n");
printf("m - dump the content of the RAM buffer\r\n");
printf("mc - clear the RAM buffer\r\n");
}
}
}
}
//return 0;
} // main
//
// Set up a memory regions to access GPIO
//
int setup_io()
{
int mem_fd;
uint8_t *gpio_mem;
uint32_t peri_base;
uint32_t gpio_base;
unsigned char buf[4];
FILE *fp;
char buffer[1024];
char hardware[1024];
int found = 0;
// determine peri_base
if ((fp = fopen("/proc/device-tree/soc/ranges", "rb")) != NULL) {
// get peri base from device tree
fseek(fp, 4, SEEK_SET);
if (fread(buf, 1, sizeof buf, fp) == sizeof buf) {
peri_base = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3] << 0;
}
fclose(fp);
} else {
// guess peri base based on /proc/cpuinfo hardware field
if ((fp = fopen("/proc/cpuinfo", "r")) == NULL)
return SETUP_CPUINFO_FAIL;
while(!feof(fp) && !found) {
fgets(buffer, sizeof(buffer), fp);
sscanf(buffer, "Hardware : %s", hardware);
if (strcmp(hardware, "BCM2708") == 0 || strcmp(hardware, "BCM2835") == 0) {
// pi 1 hardware
peri_base = BCM2708_PERI_BASE;
found = 1;
} else if (strcmp(hardware, "BCM2709") == 0 || strcmp(hardware, "BCM2836") == 0) {
// pi 2 hardware
peri_base = BCM2709_PERI_BASE;
found = 1;
}
}
fclose(fp);
if (!found)
return SETUP_NOT_RPI_FAIL;
}
gpio_base = peri_base + GPIO_BASE_OFFSET;
/* open /dev/mem */
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
printf("can't open /dev/mem \n");
exit(-1);
}
if ((gpio_mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
return SETUP_MALLOC_FAIL;
if ((uint32_t)gpio_mem % PAGE_SIZE)
gpio_mem += PAGE_SIZE - ((uint32_t)gpio_mem % PAGE_SIZE);
/* mmap GPIO */
gpio_map = mmap(
(caddr_t)gpio_mem, //Any adddress in our space will do
BLOCK_SIZE, //Map length
PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory
MAP_SHARED|MAP_FIXED, // SHARED with other processes
mem_fd, //File to map
gpio_base //Offset to GPIO peripheral
);
close(mem_fd); //No need to keep mem_fd open after mmap
if (gpio_map == MAP_FAILED) {
printf("mmap error %d\n", (int)gpio_map);//errno also set!
exit(-1);
}
// Always use volatile pointer!
gpio = (volatile unsigned *)gpio_map;
} // setup_io