forked from lkesteloot/trs80emu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.go
262 lines (220 loc) · 6.03 KB
/
vm.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
// Copyright 2012 Lawrence Kesteloot
package main
// The VM (Virtual Machine) represents the entire machine.
import (
"github.com/remogatto/z80"
"io/ioutil"
"log"
"time"
)
const (
// CPU clock on the Model III: 2.02752 MHz.
cpuHz = 2027520
// Nanoseconds per clock cycle.
cpuPeriodNs = 1000000000 / cpuHz
// How many instructions to keep around in a queue so that we can display
// the last historicalPcCount instructions when a problem happens.
historicalPcCount = 20
)
// The vm structure (Virtual Machine) represents the entire emulated machine.
// That includes the CPU, memory, disk, cassette, keyboard, display, and other
// parts like the clock interrupt hardware.
type vm struct {
// The CPU state.
z80 *z80.Z80
// All of addressable memory, including ROM, RAM, and memory-mapped I/O
// such as the display.
memory []byte
// Whether each byte of RAM has been initialized. This is useful for
// finding bugs in the emulator. If a program reads too many uninitialized
// locations, then it has probably gone off the rails.
memInit []bool
// Size of ROM, starting at memory location zero.
romSize uint16
// Which IRQs should be handled.
irqMask byte
// Which IRQs have been requested by the hardware.
irqLatch byte
// Which NMIs should be handled.
nmiMask byte
// Which NMIs have been requested by the hardware.
nmiLatch byte
// Whether we've seen this NMI and handled it.
nmiSeen bool
// Simulated keyboard.
keyboard keyboard
// Floppy disk controller.
fdc fdc
// Cassette controller..
cc cassetteController
// Breakpoints.
breakpoints breakpoints
// Queued up events.
events events
// Clock from boot, in cycles.
clock uint64
// Various I/O settings.
modeImage byte
// Channel to get updates from. The VM will send updates (screen
// writes, diagnostic messages, etc.) to this channel.
vmUpdateCh chan<- vmUpdate
// Keep last "historicalPcCount" PCs for debugging.
historicalPc [historicalPcCount]uint16
// Points to the most recent instruction added.
historicalPcPtr int
// Debug message. This is constructed during instruction execution
// and logged after the instruction is done.
msg string
// Various fields to periodically debug or adjust the VM.
previousDumpTime time.Time
previousDumpClock uint64
sleptSinceDump time.Duration
startTime int64
previousAdjustClock uint64
previousTimerClock uint64
}
// Command to the VM from the UI, such as keyboard presses or boot.
type vmCommand struct {
Cmd string
Addr int
Data string
}
// Information about changes to the CPU or computer.
type vmUpdate struct {
Cmd string
Msg string
Addr int
Data int
}
// Creates a new virtual machine. Updates will be sent to vmUpdateCh.
func createVm(vmUpdateCh chan<- vmUpdate) *vm {
// Allocate memory.
memorySize := 1024 * 64
memory := make([]byte, memorySize)
memInit := make([]bool, memorySize)
log.Printf("Memory has %d bytes", len(memory))
// Load ROM into memory.
romFilename := "roms/model3.rom"
rom, err := ioutil.ReadFile(romFilename)
if err != nil {
panic(err)
}
log.Printf("ROM has %d bytes", len(rom))
// Copy ROM into memory.
copy(memory, rom)
// Make a CPU.
vm := &vm{
z80: nil, // Set below.
memory: memory,
memInit: memInit,
romSize: uint16(len(rom)),
vmUpdateCh: vmUpdateCh,
modeImage: 0x80,
}
vm.z80 = z80.NewZ80(vm, vm)
vm.z80.Reset()
return vm
}
// Starts a VM. This doesn't boot the machine. It needs to get the
// boot command from the command channel, specified in vmCommandCh.
// The command channel also includes keyboard updates.
func (vm *vm) run(vmCommandCh <-chan vmCommand) {
running := false
shutdown := false
// Handle a command from the UI.
handleCmd := func(msg vmCommand) {
switch msg.Cmd {
case "boot":
vm.reset(true)
running = true
case "reset":
vm.reset(false)
case "shutdown":
shutdown = true
case "press", "release":
vm.keyboard.keyEvent(msg.Data, msg.Cmd == "press")
case "add_breakpoint":
vm.breakpoints.add(breakpoint{pc: uint16(msg.Addr), active: true})
log.Printf("Breakpoint added at %04X", msg.Addr)
case "tron":
printDebug = !printDebug
if vm.vmUpdateCh != nil {
if printDebug {
vm.vmUpdateCh <- vmUpdate{Cmd: "message", Msg: "Trace is on"}
} else {
vm.vmUpdateCh <- vmUpdate{Cmd: "message", Msg: "Trace is off"}
}
}
case "set_disk0":
log.Printf("Loading diskette %s into drive %d", msg.Data, 0)
err := vm.loadDisk(0, msg.Data)
if err != nil {
panic(err)
}
case "set_disk1":
log.Printf("Loading diskette %s into drive %d", msg.Data, 1)
err := vm.loadDisk(1, msg.Data)
if err != nil {
panic(err)
}
case "set_cassette":
log.Printf("Loading cassette %s", msg.Data)
vm.cc.filename = msg.Data
default:
panic("Unknown VM command " + msg.Cmd)
}
}
for !shutdown {
if running {
select {
case msg := <-vmCommandCh:
handleCmd(msg)
default:
// See if there's a breakpoint here.
bp := vm.breakpoints.find(vm.z80.PC())
if bp != nil {
if vm.vmUpdateCh != nil {
vm.vmUpdateCh <- vmUpdate{Cmd: "breakpoint", Addr: int(vm.z80.PC())}
}
log.Printf("Breakpoint at %04X", vm.z80.PC())
vm.logHistoricalPc()
running = false
} else {
vm.step()
}
}
} else {
handleCmd(<-vmCommandCh)
}
}
log.Print("VM shut down")
if vm.vmUpdateCh != nil {
vm.vmUpdateCh <- vmUpdate{Cmd: "shutdown"}
}
// No more updates.
close(vm.vmUpdateCh)
vm.vmUpdateCh = nil
}
// Log the last historicalPcCount assembly instructions that we executed.
func (vm *vm) logHistoricalPc() {
for i := 0; i < historicalPcCount; i++ {
pc := vm.historicalPc[(vm.historicalPcPtr+i+1)%historicalPcCount]
line, _ := vm.disasm(pc)
log.Print(line)
}
}
// Reset the virtual machine, optionally to power-on state.
func (vm *vm) reset(powerOn bool) {
vm.resetCassette()
vm.diskInit(powerOn)
vm.setIrqMask(0)
vm.setNmiMask(0)
vm.keyboard.clearKeyboard()
vm.timerInterrupt(false)
if powerOn {
vm.z80.Reset()
vm.startTime = time.Now().UnixNano()
} else {
vm.resetButtonInterrupt(true)
}
}