/
util.go
196 lines (177 loc) · 5.56 KB
/
util.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
package main
/*
Author: Jeff Berkowitz
Copyright (C) 2024 Jeff Berkowitz
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/.
*/
import (
"encoding/binary"
"fmt" // fmt.Errorf only
"io"
"os"
"os/exec"
)
// Get the bits from hi:lo inclusive as a small uint16
// Example: w := 0xFDFF ; w.bits(10,8) == uint16(5)
func (w word) bits(hi int, lo int) uint16 {
return uint16(w>>lo) & uint16(1<<(hi-lo+1)-1)
}
// Virtual to physical address translation. There are two MMUs, one for
// kernel and one for user mode. Each MMU is at offset 32 in the respective
// arrays of 64 SPRs. The first 16 entries map 64k words (128k bytes) of
// code space. The second 16 SPRs map 64k bytes of data space. Physical
// addresses are 24 bits long, allowing 16Mib of physical data memory.
//
// The lower 12 bits of virtual address become part of the physical address.
// The upper 4 bits of virtual address are used to select one of the 16 MMU
// registers for that (mode, kind) pair. The lower 12 bits of the selected
// MMU register become the upper 12 bits of the 24-bit physical address.
//
// Since physical memory is implemented as an array of shortwords, data
// addresses are shifted right one to make up for the automatic address
// scaling that results from indexing the uint16 array. Byte accesses within
// this word must be handled by the caller.
//
// It's cheesy using a bool for the 2-element enum {code, data}. But adding
// to that enum would require a major change to the WUT-4 architecture, i.e.
// this would be the least of my worries.
func (w4 *w4machine) translate(isData bool, virtAddr word) (exception, physaddr) {
sprOffset := 32
if isData {
sprOffset += 16
}
sprOffset += int(virtAddr >> 12)
mmu := w4.reg[w4.mode].spr
upper := physaddr(mmu[sprOffset] & 0xFFF)
lower := physaddr(virtAddr & 0xFFF)
result := (upper << 12) | lower
if isData {
result >>= 1
}
// Prevent the emulator from crashing if the emulated code accesses
// past the end of physmem. TODO: memory protection architecture.
if result > PhysMemSize {
return ExMemory, result
}
return ExNone, result
}
// Reset the simulated hardware
func (w4 *w4machine) reset() {
w4.cyc = 0
w4.pc = 0
w4.run = true
w4.mode = Kern
w4.ex = 0
w4.en = false
// After initialiation, we'll want to enter user mode at user address
// 0 by executing an RTI instruction. This will restore the mode from
// the kernel mode SPR "Imr". We need this register to contain "user
// mode" when that RTI happens. I don't know what I'd do about this in
// real hardware if I do that. Should the IMR be writable?
w4.reg[Kern].spr[Imr] = User
}
// Decode a sign extended 10 or 7 bit immediate value from the current
// instruction. If the instruction doesn't have an immediate value, then
// the rest of the decode shouldn't try to use it so the return value is
// not important. In this case return the most harmless value, 0.
func (w4 *w4machine) sxtImm() uint16 {
var result uint16
ir := w4.ir
op := ir.bits(15, 13)
neg := ir.bits(12, 12) != 0
if op < 6 { // ldw, ldb, stw, stb, beq, adi all have 7-bit immediates
result = ir.bits(12, 6)
if neg {
result |= 0xFF80
}
} else if op == 6 { // lui has a 10-bit immediate, upper bits
result = ir.bits(12, 3) << 6
} else if op == 7 && !neg { // jlr - 7-bit immediate if positive
result = ir.bits(12, 6)
}
// else bits(15,12) == 0xF and the instruction has no immediate value
return result
}
// Load a binary into memory. This consumes binaries written directly
// by customasm. Each binary has exactly 1 code segment of up to 64k
// words (128k bytes) optionally followed by 1 64k byte data segement.
// If the data segment is present, the code segment is filled with
// zeroes to 128k. If the mode is 0 (kernel), the file is loaded at
// physical 0. If it is 1 (user), it's loaded at physical 3*64k byte.
func (w4 *w4machine) load(mode int, binPath string) error {
f, err := os.Open(binPath)
if err != nil {
return err
}
defer f.Close()
maxSizeBytes := 3 * 64 * K
fi, err := f.Stat()
if err != nil {
return err
}
size := int(fi.Size())
if size > maxSizeBytes {
return fmt.Errorf("not a binary: %s", binPath)
}
off := 0
if mode == User {
off += maxSizeBytes / 2
}
var b []byte = []byte{0}
var nRead int
for {
n, err := f.Read(b)
if err != nil && err != io.EOF {
break
}
if n == 0 {
break
}
if nRead&1 == 0 {
physmem[off] = word(b[0])
} else {
physmem[off] |= word(b[0]) << 8
off++
}
nRead++
}
if err == io.EOF {
err = nil
}
if err != nil {
return err
}
if nRead != size {
return fmt.Errorf("load didn't read the entire file")
}
return nil
}
func (w4 *w4machine) core(corePath string) error {
f, err := os.Create(corePath)
if err != nil {
return err
}
defer f.Close()
return binary.Write(f, binary.LittleEndian, physmem)
}
func rundis(arg string) string {
disCmd := exec.Command("../dsm/dsm", "-f", arg)
disOut, err := disCmd.Output()
if err != nil {
return err.Error()
}
result := string(disOut)
if result[len(result)-1:] == "\n" {
result = result[0:len(result)-1]
}
return result
}