Skip to content

Instantly share code, notes, and snippets.

@salexkidd
Created November 27, 2015 02:12
Show Gist options
  • Save salexkidd/2263b06d38f50f51f214 to your computer and use it in GitHub Desktop.
Save salexkidd/2263b06d38f50f51f214 to your computer and use it in GitHub Desktop.
root@raspberrypi:~# cat ./cpu6502.py
#!/usr/bin/env python
"""
run as root :)
"""
import RPi.GPIO as GPIO
import signal, sys, os, time
GPIO.setmode(GPIO.BCM)
ADDRESS_PINS = [
4, 17, 27, 22, 10, 9, 11, 5, 6, 13, 19, 26, 21, 20, 16, 12
]
RESET_PIN = 8
CLOCK_PIN = 7
def terminate(signum, frame):
GPIO.cleanup()
sys.exit(0)
def setup():
# Address pins
for pin in ADDRESS_PINS:
GPIO.setup(pin, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
# Reset pin
GPIO.setup(RESET_PIN, GPIO.OUT)
# Clock PIN (PHI2)
GPIO.setup(CLOCK_PIN, GPIO.OUT)
# SIGINT
signal.signal(signal.SIGINT, terminate)
def reset():
GPIO.output(RESET_PIN, GPIO.HIGH)
time.sleep(0.1)
GPIO.output(RESET_PIN, GPIO.LOW)
time.sleep(0.1)
GPIO.output(RESET_PIN, GPIO.HIGH)
print("reset complete")
def send_clock():
time.sleep(0.1)
GPIO.output(CLOCK_PIN, GPIO.LOW)
time.sleep(0.1)
GPIO.output(CLOCK_PIN, GPIO.HIGH)
def read_address():
pin_state = ""
for pin in reversed(ADDRESS_PINS):
pin_state += str(GPIO.input(pin))
hex_address = hex(int(pin_state, 2))
return pin_state, hex_address
def main_loop():
while True:
address = read_address()
input = None
#input = raw_input('("reset", (n)ext, "(r)ead" [{}]> '.format(address))
if input == "reset":
reset()
elif input == "r":
print("READ!: [{}]".format(read_address()))
else:
print("READ!: [{}]".format(read_address()))
send_clock()
if __name__ == "__main__":
setup()
reset()
main_loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment