-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmk14memmap.py
More file actions
183 lines (168 loc) · 6.55 KB
/
mk14memmap.py
File metadata and controls
183 lines (168 loc) · 6.55 KB
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
#!/usr/bin/python3
# part of the MK14 emulator in python
# it handles the memory mapping
#
# The init function takes a list containing address and memory blocks
# The address block gives the address of the first byte and the memory block contains bytes to be loaded.
# Note:- during the init phase the ROM protection is disabled.
#
# getDisplaySegments - returns the status of a display byte
#
# doButtonMap - sets/unsets the bit in the address space for buttons.
#
# read - reads a byte from memory
# should also handle keyboard
# and IO chip stuff. TODO
#
# write - writes a byte to memory
# handle the updating of display
# and IO chip stuff. TODO
#
#
#
class MK14_MEMMAP:
def __init__(self, getCyclesFn):
self.getCyclesFn = getCyclesFn
self.stdRam = [0] * 256 # standard ram
self.extRam = [0] * 256 # extended ram
self.ioRam = [0] *128 # Ram in the IO chip
self.display = [0] * 8
self.dispSetCycTime = [0] * 8
self.btnMap = [0xff] * 8
self.rom = [0] * 512 # Rom
def getDisplaySegments(self, n):
"""
returns the status of a display byte - for mk14ui
"""
return (self.display[n], self.dispSetCycTime[n])
def doButtonMap(self, addr, bit, isPressed):
"""
sets the bitmap line
"""
if isPressed:
mask = (1 << bit) ^ 0xff
self.btnMap[addr] &= mask
else:
mask = 1 << bit
self.btnMap[addr] |= mask
# print("ButtonAddr", addr, self.btnMap[addr], isPressed)
def setButton(self, buttonStr, isPressed):
"""
sets bit stuff based on button pressed outside in mk14ui
"""
if buttonStr == "Abort":
self.doButtonMap(4, 5, isPressed)
elif buttonStr == "Term":
self.doButtonMap(7, 5, isPressed)
elif buttonStr == "Mem":
self.doButtonMap(3, 5, isPressed)
elif buttonStr == "Go":
self.doButtonMap(2, 5, isPressed)
else:
buttonCode = int(buttonStr, 16)
if buttonCode < 8:
self.doButtonMap(buttonCode, 7, isPressed)
elif buttonCode < 10:
self.doButtonMap(buttonCode-8, 6, isPressed)
elif buttonCode < 0x0e:
self.doButtonMap(buttonCode - 10, 4, isPressed)
else:
self.doButtonMap(buttonCode - 10 + 2, 4, isPressed)
def read(self, addr):
"""
read from memory
addr - address to read from
returns byte
"""
page = addr & 0xf00
if page == 0xf00:
# F00-FFF 256 bytes RAM (Standard)
# return self.stdRam[addr - 0xf00]
# DA March 2020 changed to use and
return self.stdRam[addr & 0xff]
if page == 0xb00:
# B00-BFF 256 bytes RAM (Extended) DA March 2020 added extended ram
# return self.extRam[addr - 0xb00]
# DA March 2020 changed to use and
return self.extRam[addr & 0xff]
elif page == 0x900 or page == 0xd00:
# 0x900 and 0xd00 are display and keyboard memory areas
if addr & 0x0f <= 0x07:
# print("Read keyboard", addr, "rslt", self.btnMap[addr & 0x07])
return self.btnMap[addr & 0x07]
else:
return 0xff
elif page < 0x800:
# DA March 2020 changed from 0x900 to 0x800
# 000 - 7FF is rom repeated 4 times
return self.rom[addr & 0x01ff]
else:
# DA March 2020 - add the RAM IO memory and IO stuff
# should be the RAM IO are
# 0x800 0xa00 0xc00 0xe00
if addr & 0xff <= 0x7f:
# 128 bytes of ram
return self.ioRam[addr & 0x7f]
else:
return 0
# need to handle the IO device stuff.
# print("Read address unknown", format(addr, "04x"))
return 0
def write(self, addr, val, overwriteROM = False):
"""
write to memory and other areas
"""
page = addr & 0xf00
if page == 0xf00:
# F00-FFF 256 bytes RAM (Standard)
# self.stdRam[addr - 0xf00] = val & 0xff
# DA March 2020 change do use bit and to get array entry number
self.stdRam[addr & 0xff] = val & 0xff
elif page==0xb00:
# DA March 2020 added extended
# B00-BFF 256 bytes RAM (Extended) DA March 2020 added extended ram
self.extRam[addr & 0xff] = val & 0xff
elif page == 0x900 or page == 0xd00:
# 0x900 and 0xd00 are display and keyboard memory areas
if addr & 0x0f <= 0x07:
self.display[addr & 0x07] = val
self.dispSetCycTime[addr & 0x07] = self.getCyclesFn()
# print("Disp: ", end="")
# for i in range(8):
# print(format(self.display[7-i],"02x"),"",end="")
# print()
elif page < 0x800:
# DA March 2020 changed from 0x900 to 0x800
# 000 - 7FF is rom repeated 4 times
# DA March 2020 - added write protect test
if overwriteROM:
self.rom[addr & 0x01ff] = val & 0xff
else:
print("ROM write attempt ", format(addr, "04x"))
else:
# DA March 2020 - add the RAM IO memory and IO stuff
# should be the RAM IO are
# 0x800 0xa00 0xc00 0xe00
if addr & 0xff <= 0x7f:
# 128 bytes of ram
self.ioRam[addr & 0x7f] = val & 0xff
else:
print ("IO data")
# need to handle the IO device stuff.
# print("Write address unknown", format(addr, "04x"))
def init(self, initialList):
"""
Initialise the memory
Args
initialList - defines the contents to write to memory
each entry has an address element
followed by a data element
which is a number of bytes
"""
for memBlock in initialList:
# get the details of the data to be stored
blkAddr = memBlock[0]
blkData = memBlock[1]
for i in range(len(blkData)):
# store byte - set overwriteROM to allow ROM to be set.
self.write(blkAddr+i, blkData[i], True)