]> git.sesse.net Git - pistorm/blob - emulator.c
Merge pull request #1 from beeanyew/minor-optimizations
[pistorm] / emulator.c
1 #include <assert.h>
2 #include <dirent.h>
3 #include <endian.h>
4 #include <fcntl.h>
5 #include <pthread.h>
6 #include <sched.h>
7 #include <signal.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/mman.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16 #include <sys/ioctl.h>
17 #include "Gayle.h"
18 #include "ide.h"
19 #include "m68k.h"
20 #include "main.h"
21 #include "platforms/platforms.h"
22 #include "input/input.h"
23
24 #include "platforms/amiga/amiga-registers.h"
25
26 //#define BCM2708_PERI_BASE        0x20000000  //pi0-1
27 //#define BCM2708_PERI_BASE     0xFE000000     //pi4
28 #define BCM2708_PERI_BASE 0x3F000000  // pi3
29 #define BCM2708_PERI_SIZE 0x01000000
30 #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
31 #define GPCLK_BASE (BCM2708_PERI_BASE + 0x101000)
32 #define GPIO_ADDR 0x200000 /* GPIO controller */
33 #define GPCLK_ADDR 0x101000
34 #define CLK_PASSWD 0x5a000000
35 #define CLK_GP0_CTL 0x070
36 #define CLK_GP0_DIV 0x074
37
38 #define SA0 5
39 #define SA1 3
40 #define SA2 2
41
42 #define STATUSREGADDR  \
43   GPIO_CLR = 1 << SA0; \
44   GPIO_CLR = 1 << SA1; \
45   GPIO_SET = 1 << SA2;
46 #define W16            \
47   GPIO_CLR = 1 << SA0; \
48   GPIO_CLR = 1 << SA1; \
49   GPIO_CLR = 1 << SA2;
50 #define R16            \
51   GPIO_SET = 1 << SA0; \
52   GPIO_CLR = 1 << SA1; \
53   GPIO_CLR = 1 << SA2;
54 #define W8             \
55   GPIO_CLR = 1 << SA0; \
56   GPIO_SET = 1 << SA1; \
57   GPIO_CLR = 1 << SA2;
58 #define R8             \
59   GPIO_SET = 1 << SA0; \
60   GPIO_SET = 1 << SA1; \
61   GPIO_CLR = 1 << SA2;
62
63 #define PAGE_SIZE (4 * 1024)
64 #define BLOCK_SIZE (4 * 1024)
65
66 #define GPIOSET(no, ishigh) \
67   do {                      \
68     if (ishigh)             \
69       set |= (1 << (no));   \
70     else                    \
71       reset |= (1 << (no)); \
72   } while (0)
73
74 #define FASTBASE 0x07FFFFFF
75 #define FASTSIZE 0xFFFFFFF
76 #define GAYLEBASE 0xD80000  // D7FFFF
77 #define GAYLESIZE 0x6FFFF
78
79 #define JOY0DAT 0xDFF00A
80 #define JOY1DAT 0xDFF00C
81 #define CIAAPRA 0xBFE001
82 #define POTGOR  0xDFF016
83
84 int kb_hook_enabled = 0;
85 int mouse_hook_enabled = 0;
86 int cpu_emulation_running = 1;
87
88 char mouse_dx = 0, mouse_dy = 0;
89 char mouse_buttons = 0;
90
91 #define KICKBASE 0xF80000
92 #define KICKSIZE 0x7FFFF
93
94 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
95 int mem_fd_gpclk;
96 int gayle_emulation_enabled = 1;
97 void *gpio_map;
98 void *gpclk_map;
99
100 // Configurable emulator options
101 unsigned int cpu_type = M68K_CPU_TYPE_68000;
102 unsigned int loop_cycles = 300;
103 struct emulator_config *cfg = NULL;
104 char keyboard_file[256] = "/dev/input/event0";
105
106 // I/O access
107 volatile unsigned int *gpio;
108 volatile unsigned int *gpclk;
109 volatile unsigned int gpfsel0;
110 volatile unsigned int gpfsel1;
111 volatile unsigned int gpfsel2;
112 volatile unsigned int gpfsel0_o;
113 volatile unsigned int gpfsel1_o;
114 volatile unsigned int gpfsel2_o;
115
116 // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or
117 // SET_GPIO_ALT(x,y)
118 #define INP_GPIO(g) *(gpio + ((g) / 10)) &= ~(7 << (((g) % 10) * 3))
119 #define OUT_GPIO(g) *(gpio + ((g) / 10)) |= (1 << (((g) % 10) * 3))
120 #define SET_GPIO_ALT(g, a)  \
121   *(gpio + (((g) / 10))) |= \
122       (((a) <= 3 ? (a) + 4 : (a) == 4 ? 3 : 2) << (((g) % 10) * 3))
123
124 #define GPIO_SET \
125   *(gpio + 7)  // sets   bits which are 1 ignores bits which are 0
126 #define GPIO_CLR \
127   *(gpio + 10)  // clears bits which are 1 ignores bits which are 0
128
129 #define GET_GPIO(g) (*(gpio + 13) & (1 << g))  // 0 if LOW, (1<<g) if HIGH
130
131 #define GPIO_PULL *(gpio + 37)      // Pull up/pull down
132 #define GPIO_PULLCLK0 *(gpio + 38)  // Pull up/pull down clock
133
134 void setup_io();
135
136 uint32_t read8(uint32_t address);
137 void write8(uint32_t address, uint32_t data);
138
139 uint32_t read16(uint32_t address);
140 void write16(uint32_t address, uint32_t data);
141
142 void write32(uint32_t address, uint32_t data);
143 uint32_t read32(uint32_t address);
144
145 uint16_t read_reg(void);
146 void write_reg(unsigned int value);
147
148 volatile uint16_t srdata;
149 volatile uint32_t srdata2;
150 volatile uint32_t srdata2_old;
151
152 //unsigned char g_kick[524288];
153 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
154 unsigned char toggle;
155 int ovl;
156 static volatile unsigned char maprom;
157
158 void sigint_handler(int sig_num) {
159   //if (sig_num) { }
160   //cpu_emulation_running = 0;
161
162   //return;
163   printf("Received sigint %d, exiting.\n", sig_num);
164   if (mouse_fd != -1)
165     close(mouse_fd);
166   if (mem_fd)
167     close(mem_fd);
168
169   exit(0);
170 }
171
172 void *iplThread(void *args) {
173   printf("IPL thread running/n");
174
175   while (42) {
176
177     if (GET_GPIO(1) == 0) {
178       toggle = 1;
179       m68k_end_timeslice();
180    //printf("thread!/n");
181     } else {
182       toggle = 0;
183     };
184     usleep(1);
185   }
186
187 }
188
189 int main(int argc, char *argv[]) {
190   int g;
191   const struct sched_param priority = {99};
192
193   // Some command line switch stuffles
194   for (g = 1; g < argc; g++) {
195     if (strcmp(argv[g], "--disable-gayle") == 0) {
196       gayle_emulation_enabled = 0;
197     }
198     else if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
199       if (g + 1 >= argc) {
200         printf("%s switch found, but no CPU type specified.\n", argv[g]);
201       } else {
202         g++;
203         cpu_type = get_m68k_cpu_type(argv[g]);
204       }
205     }
206     else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
207       if (g + 1 >= argc) {
208         printf("%s switch found, but no config filename specified.\n", argv[g]);
209       } else {
210         g++;
211         cfg = load_config_file(argv[g]);
212       }
213     }
214     else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
215       if (g + 1 >= argc) {
216         printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
217       } else {
218         g++;
219         strcpy(keyboard_file, argv[g]);
220       }
221     }
222   }
223
224   if (!cfg) {
225     printf("No config file specified. Trying to load default.cfg...\n");
226     cfg = load_config_file("default.cfg");
227     if (!cfg) {
228       printf("Couldn't load default.cfg, empty emulator config will be used.\n");
229       cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
230       if (!cfg) {
231         printf("Failed to allocate memory for emulator config!\n");
232         return 1;
233       }
234       memset(cfg, 0x00, sizeof(struct emulator_config));
235     }
236   }
237
238   if (cfg) {
239     if (cfg->cpu_type) cpu_type = cfg->cpu_type;
240     if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
241
242     if (!cfg->platform)
243       cfg->platform = make_platform_config("none", "generic");
244     cfg->platform->platform_initial_setup(cfg);
245   }
246
247   if (cfg->mouse_enabled) {
248     mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
249     if (mouse_fd == -1) {
250       printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
251       cfg->mouse_enabled = 0;
252     }
253   }
254
255   keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
256   if (keyboard_fd == -1) {
257     printf("Failed to open keyboard event source.\n");
258   }
259
260   sched_setscheduler(0, SCHED_FIFO, &priority);
261   mlockall(MCL_CURRENT);  // lock in memory to keep us from paging out
262
263   InitGayle();
264
265   signal(SIGINT, sigint_handler);
266   setup_io();
267
268   //goto skip_everything;
269
270   // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
271   // on pi model
272   printf("Enable 200MHz GPCLK0 on GPIO4\n");
273
274   *(gpclk + (CLK_GP0_CTL / 4)) = CLK_PASSWD | (1 << 5);
275   usleep(10);
276   while ((*(gpclk + (CLK_GP0_CTL / 4))) & (1 << 7))
277     ;
278   usleep(100);
279   *(gpclk + (CLK_GP0_DIV / 4)) =
280       CLK_PASSWD | (6 << 12);  // divider , 6=200MHz on pi3
281   usleep(10);
282   *(gpclk + (CLK_GP0_CTL / 4)) =
283       CLK_PASSWD | 5 | (1 << 4);  // pll? 6=plld, 5=pllc
284   usleep(10);
285   while (((*(gpclk + (CLK_GP0_CTL / 4))) & (1 << 7)) == 0)
286     ;
287   usleep(100);
288
289   SET_GPIO_ALT(4, 0);  // gpclk0
290
291   // set SA to output
292   INP_GPIO(2);
293   OUT_GPIO(2);
294   INP_GPIO(3);
295   OUT_GPIO(3);
296   INP_GPIO(5);
297   OUT_GPIO(5);
298
299   // set gpio0 (aux0) and gpio1 (aux1) to input
300   INP_GPIO(0);
301   INP_GPIO(1);
302
303   // Set GPIO pins 6,7 and 8-23 to output
304   for (g = 6; g <= 23; g++) {
305     INP_GPIO(g);
306     OUT_GPIO(g);
307   }
308   printf("Precalculate GPIO8-23 as Output\n");
309   gpfsel0_o = *(gpio);  // store gpio ddr
310   printf("gpfsel0: %#x\n", gpfsel0_o);
311   gpfsel1_o = *(gpio + 1);  // store gpio ddr
312   printf("gpfsel1: %#x\n", gpfsel1_o);
313   gpfsel2_o = *(gpio + 2);  // store gpio ddr
314   printf("gpfsel2: %#x\n", gpfsel2_o);
315
316   // Set GPIO pins 8-23 to input
317   for (g = 8; g <= 23; g++) {
318     INP_GPIO(g);
319   }
320   printf("Precalculate GPIO8-23 as Input\n");
321   gpfsel0 = *(gpio);  // store gpio ddr
322   printf("gpfsel0: %#x\n", gpfsel0);
323   gpfsel1 = *(gpio + 1);  // store gpio ddr
324   printf("gpfsel1: %#x\n", gpfsel1);
325   gpfsel2 = *(gpio + 2);  // store gpio ddr
326   printf("gpfsel2: %#x\n", gpfsel2);
327
328   GPIO_CLR = 1 << 2;
329   GPIO_CLR = 1 << 3;
330   GPIO_SET = 1 << 5;
331
332   GPIO_SET = 1 << 6;
333   GPIO_SET = 1 << 7;
334
335   // reset cpld statemachine first
336
337   write_reg(0x01);
338   usleep(100);
339   usleep(1500);
340   write_reg(0x00);
341   usleep(100);
342
343   // reset amiga and statemachine
344   skip_everything:;
345   cpu_pulse_reset();
346   ovl = 1;
347   m68k_write_memory_8(0xbfe201, 0x0001);  // AMIGA OVL
348   m68k_write_memory_8(0xbfe001, 0x0001);  // AMIGA OVL high (ROM@0x0)
349
350   usleep(1500);
351
352   m68k_init();
353   printf("Setting CPU type to %d.\n", cpu_type);
354   m68k_set_cpu_type(cpu_type);
355   m68k_pulse_reset();
356
357   if (maprom == 1) {
358     m68k_set_reg(M68K_REG_PC, 0xF80002);
359   } else {
360     m68k_set_reg(M68K_REG_PC, 0x0);
361   }
362
363 /*
364           pthread_t id;
365           int err;
366           err = pthread_create(&id, NULL, &iplThread, NULL);
367           if (err != 0)
368               printf("\ncan't create IPL thread :[%s]", strerror(err));
369           else
370               printf("\n IPL Thread created successfully\n");
371 */
372   char c = 0;
373
374   m68k_pulse_reset();
375   while (42) {
376     if (mouse_hook_enabled) {
377       if (get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons)) {
378         //printf("Maus: %d (%.2X), %d (%.2X), B:%.2X\n", mouse_dx, mouse_dx, mouse_dy, mouse_dy, mouse_buttons);
379       }
380     }
381
382     if (cpu_emulation_running)
383       m68k_execute(loop_cycles);
384     
385     // FIXME: Rework this to use keyboard events instead.
386     /*while (get_key_char(&c)) {
387       if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
388         kb_hook_enabled = 1;
389         printf("Keyboard hook enabled.\n");
390       }
391       else if (c == 0x1B && kb_hook_enabled) {
392         kb_hook_enabled = 0;
393         printf("Keyboard hook disabled.\n");
394       }
395       if (!kb_hook_enabled) {
396         if (c == cfg->mouse_toggle_key) {
397           mouse_hook_enabled ^= 1;
398           printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
399           mouse_dx = mouse_dy = mouse_buttons = 0;
400         }
401         if (c == 'r') {
402           cpu_emulation_running ^= 1;
403           printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
404         }
405         if (c == 'R') {
406           cpu_pulse_reset();
407           m68k_pulse_reset();
408           printf("CPU emulation reset.\n");
409         }
410         if (c == 'q') {
411           printf("Quitting and exiting emulator.\n");
412           goto stop_cpu_emulation;
413         }
414       }
415     }*/
416 /*
417     if (toggle == 1){
418       srdata = read_reg();
419       m68k_set_irq((srdata >> 13) & 0xff);
420     } else {
421          m68k_set_irq(0);
422     };
423     usleep(1);
424 */
425
426
427     if (GET_GPIO(1) == 0) {
428       srdata = read_reg();
429       m68k_set_irq((srdata >> 13) & 0xff);
430     } else {
431       if (CheckIrq() == 1) {
432         write16(0xdff09c, 0x8008);
433         m68k_set_irq(2);
434       }
435       else
436          m68k_set_irq(0);
437     };
438
439   }
440
441   stop_cpu_emulation:;
442
443   if (mouse_fd != -1)
444     close(mouse_fd);
445   if (mem_fd)
446     close(mem_fd);
447
448   return 0;
449 }
450
451 void cpu_pulse_reset(void) {
452   write_reg(0x00);
453   // printf("Status Reg%x\n",read_reg());
454   usleep(100000);
455   write_reg(0x02);
456   // printf("Status Reg%x\n",read_reg());
457 }
458
459 int cpu_irq_ack(int level) {
460   printf("cpu irq ack\n");
461   return level;
462 }
463
464 static unsigned int target = 0;
465
466 #define PLATFORM_CHECK_READ(a) \
467   if (address >= cfg->custom_low && address < cfg->custom_high) { \
468     unsigned int target = 0; \
469     switch(cfg->platform->id) { \
470       case PLATFORM_AMIGA: { \
471         if (custom_read_amiga(cfg, address, &target, a) != -1) { \
472           return target; \
473         } \
474         break; \
475       } \
476       default: \
477         break; \
478     } \
479   } \
480   if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
481     if (handle_mapped_read(cfg, address, &target, a) != -1) \
482       return target; \
483   }
484
485 unsigned int m68k_read_memory_8(unsigned int address) {
486   PLATFORM_CHECK_READ(OP_TYPE_BYTE);
487
488     address &=0xFFFFFF;
489 //  if (address < 0xffffff) {
490     return read8((uint32_t)address);
491 //  }
492
493 //  return 1;
494 }
495
496 unsigned int m68k_read_memory_16(unsigned int address) {
497   PLATFORM_CHECK_READ(OP_TYPE_WORD);
498
499   if (mouse_hook_enabled) {
500     if (address == JOY0DAT) {
501       // Forward mouse valueses to Amyga.
502       unsigned short result = (mouse_dy << 8) | (mouse_dx);
503       mouse_dx = mouse_dy = 0;
504       return (unsigned int)result;
505     }
506     if (address == CIAAPRA) {
507       unsigned short result = (unsigned int)read16((uint32_t)address);
508       if (mouse_buttons & 0x01) {
509         mouse_buttons -= 1;
510         return (unsigned int)(result | 0x40);
511       }
512       else
513           return (unsigned int)result;
514     }
515     if (address == POTGOR) {
516       unsigned short result = (unsigned int)read16((uint32_t)address);
517       if (mouse_buttons & 0x02) {
518         mouse_buttons -= 2;
519         return (unsigned int)(result | 0x2);
520       }
521       else
522           return (unsigned int)result;
523     }
524   }
525
526 //  if (address < 0xffffff) {
527     address &=0xFFFFFF;
528     return (unsigned int)read16((uint32_t)address);
529 //  }
530
531 //  return 1;
532 }
533
534 unsigned int m68k_read_memory_32(unsigned int address) {
535   PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
536
537 //  if (address < 0xffffff) {
538     address &=0xFFFFFF;
539     uint16_t a = read16(address);
540     uint16_t b = read16(address + 2);
541     return (a << 16) | b;
542 //  }
543
544 //  return 1;
545 }
546
547 #define PLATFORM_CHECK_WRITE(a) \
548   if (address >= cfg->custom_low && address < cfg->custom_high) { \
549     switch(cfg->platform->id) { \
550       case PLATFORM_AMIGA: { \
551         if (custom_write_amiga(cfg, address, value, a) != -1) { \
552           return; \
553         } \
554         break; \
555       } \
556       default: \
557         break; \
558     } \
559   } \
560   if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
561     if (handle_mapped_write(cfg, address, value, a) != -1) \
562       return; \
563   }
564
565 void m68k_write_memory_8(unsigned int address, unsigned int value) {
566   PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
567
568   if (address == 0xbfe001) {
569     if (ovl != (value & (1 << 0))) {
570       ovl = (value & (1 << 0));
571       printf("OVL:%x\n", ovl);
572     }
573   }
574
575 //  if (address < 0xffffff) {
576     address &=0xFFFFFF;
577     write8((uint32_t)address, value);
578     return;
579 //  }
580
581 //  return;
582 }
583
584 void m68k_write_memory_16(unsigned int address, unsigned int value) {
585   PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
586
587 //  if (address < 0xffffff) {
588     address &=0xFFFFFF;
589     write16((uint32_t)address, value);
590     return;
591 //  }
592 //  return;
593 }
594
595 void m68k_write_memory_32(unsigned int address, unsigned int value) {
596   PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
597
598 //  if (address < 0xffffff) {
599     address &=0xFFFFFF;
600     write16(address, value >> 16);
601     write16(address + 2, value);
602     return;
603 //  }
604
605 //  return;
606 }
607
608 inline void write16(uint32_t address, uint32_t data) {
609   uint32_t addr_h_s = (address & 0x0000ffff) << 8;
610   uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
611   uint32_t addr_l_s = (address >> 16) << 8;
612   uint32_t addr_l_r = (~address >> 16) << 8;
613   uint32_t data_s = (data & 0x0000ffff) << 8;
614   uint32_t data_r = (~data & 0x0000ffff) << 8;
615
616   //      asm volatile ("dmb" ::: "memory");
617   W16
618   *(gpio) = gpfsel0_o;
619   *(gpio + 1) = gpfsel1_o;
620   *(gpio + 2) = gpfsel2_o;
621
622   *(gpio + 7) = addr_h_s;
623   *(gpio + 10) = addr_h_r;
624   GPIO_CLR = 1 << 7;
625   GPIO_SET = 1 << 7;
626
627   *(gpio + 7) = addr_l_s;
628   *(gpio + 10) = addr_l_r;
629   GPIO_CLR = 1 << 7;
630   GPIO_SET = 1 << 7;
631
632   // write phase
633   *(gpio + 7) = data_s;
634   *(gpio + 10) = data_r;
635   GPIO_CLR = 1 << 7;
636   GPIO_SET = 1 << 7;
637
638   *(gpio) = gpfsel0;
639   *(gpio + 1) = gpfsel1;
640   *(gpio + 2) = gpfsel2;
641   while ((GET_GPIO(0)))
642     ;
643   //     asm volatile ("dmb" ::: "memory");
644 }
645
646 inline void write8(uint32_t address, uint32_t data) {
647   if ((address & 1) == 0)
648     data = data + (data << 8);  // EVEN, A0=0,UDS
649   else
650     data = data & 0xff;  // ODD , A0=1,LDS
651   uint32_t addr_h_s = (address & 0x0000ffff) << 8;
652   uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
653   uint32_t addr_l_s = (address >> 16) << 8;
654   uint32_t addr_l_r = (~address >> 16) << 8;
655   uint32_t data_s = (data & 0x0000ffff) << 8;
656   uint32_t data_r = (~data & 0x0000ffff) << 8;
657
658   //   asm volatile ("dmb" ::: "memory");
659   W8
660   *(gpio) = gpfsel0_o;
661   *(gpio + 1) = gpfsel1_o;
662   *(gpio + 2) = gpfsel2_o;
663
664   *(gpio + 7) = addr_h_s;
665   *(gpio + 10) = addr_h_r;
666   GPIO_CLR = 1 << 7;
667   GPIO_SET = 1 << 7;
668
669   *(gpio + 7) = addr_l_s;
670   *(gpio + 10) = addr_l_r;
671   GPIO_CLR = 1 << 7;
672   GPIO_SET = 1 << 7;
673
674   // write phase
675   *(gpio + 7) = data_s;
676   *(gpio + 10) = data_r;
677   GPIO_CLR = 1 << 7;
678   GPIO_SET = 1 << 7;
679
680   *(gpio) = gpfsel0;
681   *(gpio + 1) = gpfsel1;
682   *(gpio + 2) = gpfsel2;
683   while ((GET_GPIO(0)))
684     ;
685   //   asm volatile ("dmb" ::: "memory");
686 }
687
688 inline uint32_t read16(uint32_t address) {
689   volatile int val;
690   uint32_t addr_h_s = (address & 0x0000ffff) << 8;
691   uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
692   uint32_t addr_l_s = (address >> 16) << 8;
693   uint32_t addr_l_r = (~address >> 16) << 8;
694
695   //   asm volatile ("dmb" ::: "memory");
696   R16
697   *(gpio) = gpfsel0_o;
698   *(gpio + 1) = gpfsel1_o;
699   *(gpio + 2) = gpfsel2_o;
700
701   *(gpio + 7) = addr_h_s;
702   *(gpio + 10) = addr_h_r;
703   GPIO_CLR = 1 << 7;
704   GPIO_SET = 1 << 7;
705
706   *(gpio + 7) = addr_l_s;
707   *(gpio + 10) = addr_l_r;
708   GPIO_CLR = 1 << 7;
709   GPIO_SET = 1 << 7;
710
711   // read phase
712   *(gpio) = gpfsel0;
713   *(gpio + 1) = gpfsel1;
714   *(gpio + 2) = gpfsel2;
715   GPIO_CLR = 1 << 6;
716   while (!(GET_GPIO(0)))
717     ;
718   GPIO_CLR = 1 << 6;
719   val = *(gpio + 13);
720   GPIO_SET = 1 << 6;
721   //    asm volatile ("dmb" ::: "memory");
722   return (val >> 8) & 0xffff;
723 }
724
725 inline uint32_t read8(uint32_t address) {
726   int val;
727   uint32_t addr_h_s = (address & 0x0000ffff) << 8;
728   uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
729   uint32_t addr_l_s = (address >> 16) << 8;
730   uint32_t addr_l_r = (~address >> 16) << 8;
731
732   //    asm volatile ("dmb" ::: "memory");
733   R8
734   *(gpio) = gpfsel0_o;
735   *(gpio + 1) = gpfsel1_o;
736   *(gpio + 2) = gpfsel2_o;
737
738   *(gpio + 7) = addr_h_s;
739   *(gpio + 10) = addr_h_r;
740   GPIO_CLR = 1 << 7;
741   GPIO_SET = 1 << 7;
742
743   *(gpio + 7) = addr_l_s;
744   *(gpio + 10) = addr_l_r;
745   GPIO_CLR = 1 << 7;
746   GPIO_SET = 1 << 7;
747
748   // read phase
749   *(gpio) = gpfsel0;
750   *(gpio + 1) = gpfsel1;
751   *(gpio + 2) = gpfsel2;
752
753   GPIO_CLR = 1 << 6;
754   while (!(GET_GPIO(0)))
755     ;
756   GPIO_CLR = 1 << 6;
757   val = *(gpio + 13);
758   GPIO_SET = 1 << 6;
759   //    asm volatile ("dmb" ::: "memory");
760
761   val = (val >> 8) & 0xffff;
762   if ((address & 1) == 0)
763     return (val >> 8) & 0xff;  // EVEN, A0=0,UDS
764   else
765     return val & 0xff;  // ODD , A0=1,LDS
766 }
767
768 /******************************************************/
769
770 void write_reg(unsigned int value) {
771   STATUSREGADDR
772   *(gpio) = gpfsel0_o;
773   *(gpio + 1) = gpfsel1_o;
774   *(gpio + 2) = gpfsel2_o;
775   *(gpio + 7) = (value & 0xffff) << 8;
776   *(gpio + 10) = (~value & 0xffff) << 8;
777   GPIO_CLR = 1 << 7;
778   GPIO_CLR = 1 << 7;  // delay
779   GPIO_SET = 1 << 7;
780   GPIO_SET = 1 << 7;
781   // Bus HIGH-Z
782   *(gpio) = gpfsel0;
783   *(gpio + 1) = gpfsel1;
784   *(gpio + 2) = gpfsel2;
785 }
786
787 uint16_t read_reg(void) {
788   uint32_t val;
789   STATUSREGADDR
790   // Bus HIGH-Z
791   *(gpio) = gpfsel0;
792   *(gpio + 1) = gpfsel1;
793   *(gpio + 2) = gpfsel2;
794   GPIO_CLR = 1 << 6;
795   GPIO_CLR = 1 << 6;  // delay
796   GPIO_CLR = 1 << 6;
797   GPIO_CLR = 1 << 6;
798   val = *(gpio + 13);
799   GPIO_SET = 1 << 6;
800   return (uint16_t)(val >> 8);
801 }
802
803 //
804 // Set up a memory regions to access GPIO
805 //
806 void setup_io() {
807   /* open /dev/mem */
808   if ((mem_fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
809     printf("can't open /dev/mem \n");
810     exit(-1);
811   }
812
813   /* mmap GPIO */
814   gpio_map = mmap(
815       NULL,                    // Any adddress in our space will do
816       BCM2708_PERI_SIZE,       // Map length
817       PROT_READ | PROT_WRITE,  // Enable reading & writting to mapped memory
818       MAP_SHARED,              // Shared with other processes
819       mem_fd,                  // File to map
820       BCM2708_PERI_BASE        // Offset to GPIO peripheral
821   );
822
823   close(mem_fd);  // No need to keep mem_fd open after mmap
824
825   if (gpio_map == MAP_FAILED) {
826     printf("gpio mmap error %d\n", (int)gpio_map);  // errno also set!
827     exit(-1);
828   }
829
830   gpio = ((volatile unsigned *)gpio_map) + GPIO_ADDR / 4;
831   gpclk = ((volatile unsigned *)gpio_map) + GPCLK_ADDR / 4;
832
833 }  // setup_io