-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin.go
338 lines (290 loc) · 6.81 KB
/
builtin.go
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package main
// these are the more optimised versions of the chips defined earlier
// where we abstract a little more over the clock
type Builtin interface {
ClockTick()
}
type BuiltinBit struct {
in bool
out bool
load bool
}
func NewBuiltinBit() *BuiltinBit {
return &BuiltinBit{}
}
func (b *BuiltinBit) SendIn(in bool) {
b.in = in
}
func (b *BuiltinBit) SendLoad(load bool) {
b.load = load
}
func (b *BuiltinBit) Out() bool {
return b.out
}
func (b *BuiltinBit) ClockTick() {
if b.load {
b.out = b.in
}
}
type BuiltinRegister struct {
in uint16
out uint16
load bool
}
func NewBuiltinRegister() *BuiltinRegister {
return &BuiltinRegister{}
}
func (b *BuiltinRegister) SendIn(in uint16) {
b.in = in
}
func (b *BuiltinRegister) SendLoad(load bool) {
b.load = load
}
func (b *BuiltinRegister) Out() uint16 {
return b.out
}
func (b *BuiltinRegister) ClockTick() {
if b.load {
b.out = b.in
}
}
type BuiltinRAM16K struct {
in uint16
out uint16
address uint16 // 14 bits in spec
load bool
mem [16384]uint16
}
func NewBuiltinRAM16K() *BuiltinRAM16K {
return &BuiltinRAM16K{
mem: [16384]uint16{},
}
}
func (b *BuiltinRAM16K) SendIn(in uint16) {
b.in = in
}
func (b *BuiltinRAM16K) SendLoad(load bool) {
b.load = load
}
func (b *BuiltinRAM16K) SendAddress(address uint16) {
b.address = address
b.out = b.mem[b.address]
}
func (b *BuiltinRAM16K) Out() uint16 {
return b.out
}
func (b *BuiltinRAM16K) ClockTick() {
if b.load {
b.mem[b.address] = b.in
}
b.out = b.mem[b.address]
}
type BuiltinCounter struct {
in uint16
out uint16
inc bool
load bool
reset bool
}
func NewBuiltinCounter() *BuiltinCounter {
return &BuiltinCounter{}
}
func (b *BuiltinCounter) SendIn(in uint16) {
b.in = in
}
func (b *BuiltinCounter) SendInc(inc bool) {
b.inc = inc
}
func (b *BuiltinCounter) SendLoad(load bool) {
b.load = load
}
func (b *BuiltinCounter) SendReset(reset bool) {
b.reset = reset
}
func (b *BuiltinCounter) Out() uint16 {
return b.out
}
func (b *BuiltinCounter) ClockTick() {
switch {
case b.reset:
b.out = 0
case b.load:
b.out = b.in
case b.inc:
b.out = b.out + 1
}
}
type ROM32K struct {
address uint16 // 15 bit in spec
// out uint16 // from underlying ram16
rams [2]*BuiltinRAM16K
}
func NewROM32K(program []uint16) *ROM32K {
if len(program) > 32768 {
panic("ROM32K OOM on load")
}
rams := [2]*BuiltinRAM16K{
NewBuiltinRAM16K(), NewBuiltinRAM16K(),
}
for i, instr := range program {
if i < 16384 {
rams[0].mem[i] = instr
continue
}
rams[1].mem[i-16384] = instr
}
return &ROM32K{
rams: rams,
}
}
func splitaddr(address uint16) (bit1 int, last14 uint16) {
// split b.address into bit0, bit1, last 14bits
if address >= 32768 { // 2**15
address -= 32768
}
if address >= 16384 { // 2**14
bit1 = 1 // otherwise it remains 0
address -= 16384
}
last14 = address
return
}
func (b *ROM32K) SendAddress(addr uint16) {
b.address = addr
_, addrs := splitaddr(b.address)
b.rams[0].SendAddress(addrs)
b.rams[1].SendAddress(addrs)
}
func (b *ROM32K) Out() uint16 {
bit1, _ := splitaddr(b.address)
return b.rams[bit1].Out()
}
func (b *ROM32K) ClockTick() {
b.rams[0].ClockTick()
b.rams[1].ClockTick()
}
type Memory struct {
address uint16 // 15 bit in spec
ram *BuiltinRAM16K
screen Screen
keyboard Keyboard
reader TapeReader
writer TapeWriter
}
func NewMemory() *Memory {
return &Memory{
ram: NewBuiltinRAM16K(),
screen: NewScreen512x256(),
keyboard: NewSimpleKeyboard(),
reader: NewTapeReader(),
writer: NewTapeWriter(),
}
}
func (m *Memory) SendIn(in uint16) {
m.ram.SendIn(in)
m.screen.SendIn(in)
m.writer.SendIn(in)
}
// TODO: is there a bug here where load remains high on screen
// when ram.SendLoad is called?
func (m *Memory) SendLoad(load bool) {
bit1, address := splitaddr(m.address)
if bit1 == 0 {
m.ram.SendLoad(load)
return
}
// NOTE first two bits have been masked to 0 here already
// ALSO NOTE bit0 is ignored so 2**15+1 is mapped to MEM[1]
if address < 8192 { // 2**13 or 0x2000
m.screen.SendLoad(load)
return
}
switch address {
case 8192:
return // load to keyboard is ignored
case 8193:
m.reader.SendLoad(load)
case 8194:
m.writer.SendLoad(load)
default:
if load {
panic("access memory beyond 0x6002")
}
}
}
func (m *Memory) SendAddress(address uint16) {
m.address = address
_, addr := splitaddr(address)
m.ram.SendAddress(addr)
m.screen.SendAddress(addr)
}
func (m *Memory) Out() uint16 {
bit1, address := splitaddr(m.address)
if bit1 == 0 {
return m.ram.Out()
}
if address < 8192 { // 2**13 or 0x2000
return m.screen.Out()
}
switch address {
case 8192:
return m.keyboard.Out()
case 8193:
return m.reader.Out()
case 8194:
return m.writer.Out()
default:
return 0 // access beyond 0x6002 should never find anything
// an actual read here should explode but we always read even when setting A=0x6003+
}
}
func (m *Memory) ClockTick() {
m.ram.ClockTick()
// TODO: should screen/keyboard even be clocked?
m.screen.ClockTick()
m.keyboard.ClockTick()
m.reader.ClockTick()
m.writer.ClockTick()
}
type Computer struct {
cpu CPU
instr_mem *ROM32K
data_mem *Memory
}
func NewComputer(cpu CPU) *Computer {
datamem := NewMemory()
return &Computer{
cpu: cpu,
data_mem: datamem,
}
}
//When reset is 0, the program stored in the computer's
//ROM executes. When reset is 1, the execution of the
//program restarts. Thus, to start a program's
//execution, reset must be pushed "up" (1) and then
//"down" (0).
//From this point onward the user is at the mercy of
//the software. In particular, depending on the
//program's code, the screen may show some output and
//the user may be able to interact with the computer
//via the keyboard.
func (c *Computer) SendReset(reset bool) {
c.cpu.SendReset(reset)
}
func (c *Computer) ClockTick() {
if c.data_mem == nil {
panic("no ROM loaded")
}
c.cpu.SendInstr(c.instr_mem.Out())
c.data_mem.SendAddress(c.cpu.AddressM())
c.cpu.SendInM(c.data_mem.Out())
c.cpu.ClockTick()
c.data_mem.SendLoad(c.cpu.WriteM())
c.data_mem.SendIn(c.cpu.OutM())
c.data_mem.ClockTick()
c.instr_mem.SendAddress(c.cpu.PC())
c.instr_mem.ClockTick()
}
func (c *Computer) LoadProgram(rom *ROM32K) {
c.instr_mem = rom
}