-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
emulator.go
210 lines (178 loc) · 5.15 KB
/
emulator.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
/*
Copyright (C) 2019-2020 Andreas T Jonsson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package emulator
import (
"flag"
"log"
"os"
"runtime"
"time"
"github.com/andreas-jonsson/virtualxt/emulator/dialog"
"github.com/andreas-jonsson/virtualxt/emulator/memory"
"github.com/andreas-jonsson/virtualxt/emulator/peripheral"
"github.com/andreas-jonsson/virtualxt/emulator/peripheral/debug"
"github.com/andreas-jonsson/virtualxt/emulator/peripheral/disk"
"github.com/andreas-jonsson/virtualxt/emulator/peripheral/keyboard"
"github.com/andreas-jonsson/virtualxt/emulator/peripheral/pic"
"github.com/andreas-jonsson/virtualxt/emulator/peripheral/pit"
"github.com/andreas-jonsson/virtualxt/emulator/peripheral/ram"
"github.com/andreas-jonsson/virtualxt/emulator/peripheral/rom"
"github.com/andreas-jonsson/virtualxt/emulator/peripheral/smouse"
"github.com/andreas-jonsson/virtualxt/emulator/peripheral/speaker"
"github.com/andreas-jonsson/virtualxt/emulator/peripheral/video/mda"
"github.com/andreas-jonsson/virtualxt/emulator/processor/cpu"
)
var (
biosImage = "bios/pcxtbios.bin"
vbiosImage = "bios/vxtcga.bin"
)
var (
limitMIPS float64
v20cpu, man bool
)
func init() {
if p, ok := os.LookupEnv("VXT_DEFAULT_BIOS_PATH"); ok {
biosImage = p
}
if p, ok := os.LookupEnv("VXT_DEFAULT_VIDEO_BIOS_PATH"); ok {
vbiosImage = p
}
flag.BoolVar(&v20cpu, "v20", false, "Emulate NEC V20 CPU")
flag.BoolVar(&man, "m", false, "Open manual")
flag.Float64Var(&limitMIPS, "mips", 0, "Limit CPU speed")
flag.StringVar(&biosImage, "bios", biosImage, "Path to BIOS image")
flag.StringVar(&vbiosImage, "vbios", vbiosImage, "Path to EGA/VGA BIOS image")
if !mdaVideo {
flag.BoolVar(&mdaVideo, "mda", false, "Emulate MDA video in termainal mode")
}
}
func emuLoop() {
if man {
dialog.OpenManual()
return
}
bios, err := os.Open(biosImage)
if err != nil {
dialog.ShowErrorMessage(err.Error())
return
}
defer bios.Close()
videoBios, err := os.Open(vbiosImage)
if err != nil {
dialog.ShowErrorMessage(err.Error())
return
}
defer videoBios.Close()
dc := &disk.Device{BootDrive: 0xFF}
dialog.FloppyController = dc
for i, v := range dialog.DriveImages {
if v.Name != "" {
name := v.Name
bootable := false
if name[0] == '*' {
bootable = true
name = name[1:]
}
var err error
if dialog.DriveImages[i].Fp, err = os.OpenFile(name, os.O_RDWR, 0644); err != nil {
dialog.ShowErrorMessage(err.Error())
} else if err = dc.Insert(byte(i), dialog.DriveImages[i].Fp); err != nil {
dialog.ShowErrorMessage(err.Error())
} else if bootable || dc.BootDrive == 0xFF {
dc.BootDrive = byte(i)
}
}
}
if dc.BootDrive == 0xFF {
dialog.ShowErrorMessage("No boot device selected!")
return
}
if !checkBootsector(dc) {
dialog.ShowErrorMessage("The selected disk is not bootable!")
return
}
video := defaultVideoDevice()
if mdaVideo {
video = &mda.Device{}
}
debug.MuteLogging(mdaVideo)
spkr := &speaker.Device{}
peripherals := []peripheral.Peripheral{
&ram.Device{}, // RAM (needs to go first since it maps the full memory range)
&rom.Device{
RomName: "BIOS",
Base: memory.NewPointer(0xFE00, 0),
Reader: bios,
},
&rom.Device{
RomName: "Video BIOS",
Base: memory.NewPointer(0xC000, 0),
Reader: videoBios,
},
&pic.Device{}, // Programmable Interrupt Controller
&pit.Device{}, // Programmable Interval Timer
dc, // Disk Controller
video, // Video Device
spkr, // PC Speaker
&keyboard.Device{}, // Keyboard Controller
//&joystick.Device{}, // Game Port Joysticks
&smouse.Device{ // Microsoft Serial Mouse (COM1)
BasePort: 0x3F8,
IRQ: 4,
},
}
if debug.EnableDebug {
peripherals = append(peripherals, &debug.Device{})
}
var doLimit float64 = limitMIPS
if doLimit == 0 {
doLimit = 0.33
}
limitSpeed := 1000000000 / int64(1000000*doLimit)
p := cpu.NewCPU(peripherals)
defer p.Close()
p.SetV20Support(v20cpu)
p.Reset()
for !dialog.ShutdownRequested() {
var cycles int64
t := time.Now().UnixNano()
if dialog.RestartRequested() {
p.Reset()
}
step:
c, err := p.Step()
if err != nil {
log.Print(err)
return
}
if limitMIPS == 0 && spkr.TurboSwitch() {
continue
}
cycles += int64(c)
wait:
if n := time.Now().UnixNano() - t; n <= 0 {
runtime.Gosched()
goto step
} else if n < limitSpeed*cycles {
goto wait
}
}
}
func checkBootsector(dc *disk.Device) bool {
fp := dialog.DriveImages[dc.BootDrive].Fp
var sector [512]byte
fp.ReadAt(sector[:], 0)
fp.Seek(0, os.SEEK_SET)
return sector[511] == 0xAA && sector[510] == 0x55
}