-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathromram_lcd.py
238 lines (180 loc) · 6.25 KB
/
romram_lcd.py
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
# rshell -p /dev/tty.usbmodem1231401
# minicom -D /dev/tty.usbmodem1231401 -b 115200
import sys
from ansi import *
import machine
from machine import Pin, Timer
from utime import sleep
debugging = False
machine.freq(270000000)
if debugging: print(machine.freq())
is_IO = False
is_reading = False
is_writing = False
this_address = 0
this_data = 0
# Address Pins
address = []
address_pins = [2,3,4,5,6,7,8,9,10,11,12,13,14,15]
for pin_number in address_pins:
address.append(Pin(pin_number, Pin.IN))
# Data pins
datavalues = []
data_pins = [16,17,18,19,20,21,22,26]
for pin_number in data_pins:
datavalues.append(Pin(pin_number, Pin.OUT))
# Write and Read signals
Clock = Pin("GP28", Pin.OUT)
Clock_Timer = Timer()
WR = Pin("GP0", Pin.IN, Pin.PULL_UP)
RD = Pin("GP1", Pin.IN, Pin.PULL_UP)
# IO Request
IOREQ = Pin(27, Pin.IN, Pin.PULL_UP)
# LED status light
led_pin = Pin("LED", Pin.OUT)
# Clock Ticks
tick = 0
# ROM/RAM
ram_memory = [
0x3E, 0x48, #LD A,48H ("H")
0xD3, 0x0A, #OUT (0AH),A
0x3E, 0x45, #LD A,45H ("e")
0xD3, 0x0A, #OUT (0AH),A
0x3E, 0x4C, #LD A,4CH
0xD3, 0x0A, #OUT (0AH),A
0x3E, 0x4C, #LD A,4CH
0xD3, 0x0A, #OUT (0AH),A
0x3E, 0x4F, #LD A,4FH
0xD3, 0x0A, #OUT (0AH),A
0x3E, 0x20, #LD A,20H
0xD3, 0x0A, #OUT (0AH),A
0x3E, 0x57, #LD A,57H
0xD3, 0x0A, #OUT (0AH),A
0x3E, 0x4F, #LD A,4FH
0xD3, 0x0A, #OUT (0AH),A
0x3E, 0x52, #LD A,52H
0xD3, 0x0A, #OUT (0AH),A
0x3E, 0x4C, #LD A,4CH
0xD3, 0x0A, #OUT (0AH),A
0x3E, 0x44, #LD A,44H
0xD3, 0x0A, #OUT (0AH),A
0x3E, 0x21, #LD A,21H
0xD3, 0x0A, #OUT (0AH),A
0xC3, 00, 00, #JP 0000
0x76 #HALT
]
if debugging: print("Booting")
# Get the address pins
def get_address():
global tick, is_IO, is_reading, is_writing, this_address, address, this_data, datavalues, ram_memory
this_address = 0
for address_pin in reversed(address):
this_address = (this_address << 1) + address_pin.value()
#if debugging: print(this_address)
return this_address
# Set the pins to correct values
def set_pins(pins, data):
global tick, is_IO, is_reading, is_writing, this_address, address, this_data, datavalues, ram_memory
for pin in pins:
this_bit = data & 0x01
if(this_bit == 1):
pin.on()
else:
pin.off()
data = (data >> 1)
#if debugging: print(data)
# Output the Z80 values
def print_status():
global tick, is_IO, is_reading, is_writing, this_address, address, this_data, datavalues, ram_memory
# Check if IO
is_IO = (IOREQ.value()==0)
# When RD is low then z80 is reading
is_reading = (RD.value()==0)
# Check if is writing
is_writing = (WR.value()==0)
# Get the address and the data for it
this_address = get_address()
this_data = ram_memory[this_address]
#if debugging: print(top)
if debugging: print('{:8}'.format(tick), end='')
if debugging: print(bold + " IO: " + reset, abs(is_IO),end='')
if debugging: print(bold + " RD: " + reset, abs(is_reading), end='')
if debugging: print(bold + " WR: " + reset, abs(is_writing), end='')
if debugging: print(bold + " Address:" + reset + " {:#018b}".format(this_address), end='')
if debugging: print(bold + " Data:" + reset + " {:#010b}".format(this_data), end='')
if debugging: print(" {:#06x}".format(this_data), reset, end='')
if this_data < 126 and this_data >= 32:
if debugging: print(chr(this_data), end='')
else:
if debugging: print("", end='')
if debugging: print()
#if debugging: print(clearline)
# Write handler
def wr_handler(pin):
global tick, is_IO, is_reading, is_writing, this_address, address, this_data, datavalues, ram_memory
# NOT IO so is writing to memory
if(IOREQ.value()==1):
this_address = get_address()
# WAIT.value(0)
for data_pin in reversed(datavalues):
data_pin.init(mode=Pin.IN)
for data_pin in reversed(datavalues):
this_data = (this_data << 1) + data_pin.value()
try:
ram_memory[this_address] = this_data
except:
if debugging: print("OOPS! Requested to write ", this_address)
# WAIT.value(1)
if debugging: print_status()
tick = tick + 1
# Read handler
def rd_handler(pin):
global tick, is_IO, is_reading, is_writing, this_address, address, this_data, datavalues, ram_memory
#if debugging: print("IO :",IOREQ.value())
# Not IO - Reading from memory
if(IOREQ.value()==1):
this_address = get_address()
for data_pin in reversed(datavalues):
data_pin.init(mode=Pin.OUT)
this_data = ram_memory[this_address]
set_pins(datavalues, this_data)
#if debugging: print(this_data)
if debugging: print_status()
tick = tick + 1
# IO Handler
def io_handler(pin):
global tick, is_IO, is_reading, is_writing, this_address, address, this_data, datavalues, ram_memory
for data_pin in reversed(datavalues):
data_pin.init(mode=Pin.IN)
if debugging: print_status()
tick = tick + 1
# Handle clock ticks
def clock_tick(timer):
global tick
led_pin.value(1)
Clock.toggle()
sleep(0.005)
led_pin.value(0)
tick = tick + 1
if(RD.value()==0): rd_handler(RD)
if(WR.value()==0): wr_handler(WR)
if(IOREQ.value()==0): io_handler(IOREQ)
# Fill rest of the 8kb with NOP
# 8kb = 8181
# 16kb = 16382
for b in range(16382-len(ram_memory)):
ram_memory.append(0)
# Interupts
# WR.irq(handler=wr_handler, trigger=Pin.IRQ_FALLING)
# RD.irq(handler=rd_handler, trigger=Pin.IRQ_FALLING)
# IOREQ.irq(handler=io_handler, trigger=Pin.IRQ_FALLING)
# Wait
#WAIT = Pin("GP15", Pin.OUT)
#WAIT.LOW()
# Boot up procedure
if debugging: print("Memory: ", len(ram_memory))
# Read "ROM" data as binary into memory array
#with open("z80.bin", mode='rb') as file:
# ram_memory = bytearray(file.read())
# Clock input
Clock_Timer.init(mode=Timer.PERIODIC, callback=clock_tick, freq=1000)