/* * Copyright (c) 2022 Marcus Glocker * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include #include #include #include #include #include #define PIN_LATCH_MEM 12 #define PIN_CLOCK 23 #define PIN_LATCH 24 #define PIN_DATA 25 #define DELAY 100000000 /* 100ms */ #define LOW 0 #define HIGH 1 int fd_gpio; void usage(void); void delay(void); void setpin(int, int); void shiftbit(int); void shiftword(uint16_t); void latch_shift(void); void latch_mem(void); void usage(void) { extern char *__progname; printf("usage: %s \n", __progname); exit(1); } void delay(void) { struct timespec timeout; timeout.tv_nsec = DELAY; timeout.tv_sec = 0; nanosleep(&timeout, NULL); } void setpin(int pin, int state) { struct gpio_pin_op op; memset(&op, 0, sizeof(op)); op.gp_pin = pin; if (state == LOW) op.gp_value = GPIO_PIN_LOW; else op.gp_value = GPIO_PIN_HIGH; if (ioctl(fd_gpio, GPIOPINWRITE, &op) == -1) err(1, "ioctl"); } void shiftbit(int state) { /* make sure clock is low */ setpin(PIN_CLOCK, LOW); /* set data bit */ if (state == LOW) setpin(PIN_DATA, LOW); else setpin(PIN_DATA, HIGH); /* clock in data bit */ setpin(PIN_CLOCK, HIGH); delay(); setpin(PIN_CLOCK, LOW); delay(); } void shiftword(uint16_t word) { int i; uint8_t bit; for (i = 0; i < 16; i++) { bit = (word >> i & 1); if (bit) shiftbit(HIGH); else shiftbit(LOW); //latch_shift(); } } void latch_shift(void) { setpin(PIN_LATCH, HIGH); delay(); setpin(PIN_LATCH, LOW); } void latch_mem(void) { setpin(PIN_LATCH_MEM, LOW); delay(); setpin(PIN_LATCH_MEM, HIGH); } int main(int argc, char **argv) { uint8_t low; uint8_t high; uint16_t word; int i, n; int fd_binary; if (argc < 2) usage(); fd_binary = open(argv[1], O_RDONLY); if (fd_binary == -1) err(1, "open binary"); fd_gpio = open("/dev/gpio0", O_RDWR); if (fd_gpio == -1) err(1, "open gpio"); /* init */ setpin(PIN_CLOCK, LOW); setpin(PIN_DATA, LOW); setpin(PIN_LATCH, LOW); setpin(PIN_LATCH_MEM, HIGH); /* program 8-bit computer */ for (i = 0; i < 16; i++) { low = (uint8_t)i; n = read(fd_binary, &high, 1); if (n < 1) break; word = (high << 8 | low); printf("Memory Addr %d: Memory value=0x%02x (%d)\n", i, high, high); shiftword(word); latch_shift(); latch_mem(); } close(fd_binary); close(fd_gpio); return 0; }