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