-
Notifications
You must be signed in to change notification settings - Fork 13
/
csim
executable file
·282 lines (251 loc) · 6.75 KB
/
csim
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
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use Term::ReadKey;
use IO::Pty;
use Storable;
# CSCVon8 Simulator, (C) 2019 Warren Toomey, GPL3
my @RAM= (0) x 32768;
my @ROM;
my $ALUROM;
my @DecodeROM;
my $debug=0;
my $PC=0;
my $A=0;
my $B=0;
my $AH=0;
my $AL=0;
my $IR=0;
my $phase=0;
my $aluop;
my $loadop;
my $dbusop;
my $jumpop;
my $arena;
my $pcincr;
my $usreset;
# The 1-char buffer from the keyboard. undefined means no character.
my $inchar;
# By default, UART I/O goes to STDIN/OUT
my $IN= *STDIN;
my $OUT= *STDOUT;
# List of control line masks and shifts.
use constant {
ALUOP => 0x001f,
LOADOP => 0x0007, LOADSHIFT => 5,
DBUSOP => 0x0003, DBUSSHIFT => 8,
JUMPOP => 0x0007, JUMPSHIFT => 10,
ARENA => 0x0001, ARSHIFT => 13, # Active low
PCINCR => 0x0001, PCSHIFT => 14,
USRESET => 0x0001, USSHIFT => 15, # Active low
IRSHIFT => 4,
CSHIFT => 8,
VSHIFT => 9,
ZSHIFT => 10,
NSHIFT => 11,
DSHIFT => 12,
MEMRESULT => 0,
ALURESULT => 1,
UARTRESULT => 2,
};
# List of ALUop names
my @ALUop = (
"0",
"A",
"B",
"-A",
"-B",
"A+1",
"B+1",
"A-1",
"B-1",
"A+B",
"A+B+1",
"A-B",
"A-Bspecial",
"B-A",
"A-B-1",
"B-A-1",
"A*BHI",
"A*BLO",
"A/B",
"A%B",
"A<<B",
"A>>BL",
"A>>BA",
"AROLB",
"ARORB",
"A&B",
"A|B",
"A^B",
"!A",
"!B",
"A+BCD",
"A-BCD"
);
# Enable debugging, pty I/O and randomised memory
while (@ARGV > 0) {
# Set debug mode
if ($ARGV[0] eq "-d") { $debug++; shift(@ARGV); next; }
# Open up a pty
if ($ARGV[0] eq "-p") {
$OUT = new IO::Pty; $IN= $OUT;
print("pty is ", $OUT->ttyname(), "\n");
shift(@ARGV); next;
}
# Randomise the RAM
if ($ARGV[0] eq "-r") {
foreach my $i (0 .. 32767) {
$RAM[$i]= int(rand(256));
}
shift(@ARGV); next;
}
die("Usage: $0 [-d] [-p] [-r]\n-d: debug, -r: randomise RAM, -p: open pty\n");
}
# Set the UART up to read one char at a time
ReadMode('cbreak', $IN);
# Load the ALU ROM
my $ROMIN;
if (-f "alu.hex") {
$ALUROM = retrieve("alu.hex");
} else {
# Load the ROM file and store a cached version
open( $ROMIN, "<", "alu.rom" ) || die("Can't open alu.rom: $!\n");
while (<$ROMIN>) {
chomp; push( @{ $ALUROM }, map( { hex($_) } split( /\s/, $_ ) ) );
}
close($ROMIN);
store($ALUROM, "alu.hex");
}
# Load the Decode ROM
open( $ROMIN, "<", "ucode.rom" ) || die("Can't open ucode.rom: $!\n");
while (<$ROMIN>) {
chomp; push( @DecodeROM, map( { hex($_) } split( /\s+/, $_ ) ) );
}
close($ROMIN);
# Load the instruction ROM
open( $ROMIN, "<", "instr.rom" ) || die("Can't open instr.rom: $!\n");
while (<$ROMIN>) {
chomp; push( @ROM, map( { hex($_) } split( /\s+/, $_ ) ) );
}
close($ROMIN);
# Start the simulation
while (1) {
# Work out the decode ROM index
my $decodeidx= ($IR << IRSHIFT) | $phase;
# Get the microinstruction
my $uinst= $DecodeROM[ $decodeidx ];
# Decode the microinstruction
$aluop= $uinst & ALUOP;
$loadop= ($uinst>>LOADSHIFT) & LOADOP;
$dbusop= ($uinst>>DBUSSHIFT) & DBUSOP;
$jumpop= ($uinst>>JUMPSHIFT) & JUMPOP;
$arena= ($uinst>>ARSHIFT) & ARENA;
$pcincr= ($uinst>>PCSHIFT) & PCINCR;
$usreset= ($uinst>>USSHIFT) & USRESET;
printf("PC %04x IR %02x p %01x ui %04x upa %d%d%d ", $PC,
$IR, $phase, $uinst, $usreset, $pcincr, $arena) if ($debug);
# Do the ALU operation.
my $databus=0;
my ($carry, $overflow, $zero, $negative, $divbyzero);
if ($dbusop== ALURESULT) {
my $aluresult= $ALUROM->[ ($aluop<<16) | ($A<<8) | $B ];
printf("AB %02x %02x %s %04x ", $A, $B, $ALUop[$aluop], $aluresult)
if ($debug);
# Extract the flags from the result, and remove from the result
$carry= ($aluresult>>CSHIFT) & 1;
$overflow= ($aluresult>>VSHIFT) & 1;
$zero= ($aluresult>>ZSHIFT) & 1;
$negative= ($aluresult>>NSHIFT) & 1;
$divbyzero= ($aluresult>>DSHIFT) & 1;
$databus = $aluresult & 0xff;
}
# Determine the address on the address bus: AR or PC
my $address;
if ($arena==0) {
$address= ($AH<<8) | $AL;
printf("AR %02x%02x ", $AH, $AL) if ($debug);
} else {
$address= $PC;
printf("PC %04x ", $PC) if ($debug);
}
# Get the memory value
if ($dbusop== MEMRESULT) {
$databus= ($address & 0x8000) ? $RAM[$address-0x8000] : $ROM[$address];
}
# Read from UART. Wait up to 0.5 seconds,
# and if no character, "read" a NUL character.
if ($dbusop== UARTRESULT) {
# Return 0 if they were silly enough to read when nothing is there.
# Empty the buffer regardless.
$databus = defined($inchar) ? ord($inchar) : 0;
# Only reset $inchar if there is a reader on the data bus. This
# allows UARTRESULT to be asserted for several microinstructions
# before the actual read is done.
$inchar= undef if ($loadop);
}
printf("dop %x dbus %02x ", $dbusop, $databus) if ($debug);
# Load from the data bus
if ($loadop==1) { $IR= $databus; print("->IR ") if ($debug); }
if ($loadop==2) { $A= $databus; print("->A ") if ($debug); }
if ($loadop==3) { $B= $databus; print("->B ") if ($debug); }
if ($loadop==4) {
if ($address & 0x8000) { $RAM[$address-0x8000]= $databus; }
print("->RAM ") if ($debug);
}
if ($loadop==5) { $AH= $databus; print("->AH ") if ($debug); }
if ($loadop==6) { $AL= $databus; print("->AL ") if ($debug); }
if ($loadop==7) {
print($OUT chr($databus)); $|=1; # Flush the output
print("->IO ") if ($debug);
}
# Increment the PC and the phase
$PC++ if ($pcincr==1);
$phase= ($usreset==0) ? 0 : ($phase+1) & 0xf;
# Do any jumps
printf("j%d ", $jumpop) if ($jumpop && $debug);
if ($jumpop==1 && $carry) {
$PC= $address;
print("JC ") if ($debug);
}
if ($jumpop==2 && $overflow) {
$PC= $address;
print("JO ") if ($debug);
}
if ($jumpop==3 && $zero) {
$PC= $address;
print("JZ ") if ($debug);
}
if ($jumpop==4 && $negative) {
$PC= $address;
print("JN ") if ($debug);
}
if ($jumpop==5 && $divbyzero) {
$PC= $address;
print("JD ") if ($debug);
}
# If the instruction is testing for an available UART character to
# read and we have one, don't jump. If not, try to read one and
# jump if that fails. If we read one, put it in the $inchar buffer.
if ($jumpop==7 && !defined($inchar)) {
# This code if we are reading from a terminal
if (-t $IN) {
$inchar= ReadKey(0.5, $IN);
} else {
# This code for files and pipes
$inchar= getc($IN);
printf("File/pipe read %02x ", (defined($inchar) ? ord($inchar) : 0xff)) if ($debug);
}
# There was no character to read
if (!defined($inchar)) {
$PC= $address; print("JI ") if ($debug);
}
}
# Exit if PC goes to $FFFF
last if ($PC==0xffff);
print("\n") if ($debug);
}
# Clean up and exit
ReadMode('normal', $IN);
exit(0);