-
Notifications
You must be signed in to change notification settings - Fork 13
/
gen_ucode
executable file
·181 lines (145 loc) · 4.98 KB
/
gen_ucode
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
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
# CSCVon8 microcode generator, (C) 2019 Warren Toomey, GPL3
#
# Generate the ucode.rom from the microcode text file,
# and create the opcodes file. Also generate a ROM file,
# 27Cucode.rom which is ready to write to the read ROM
# using minipro.
# Hash of control lines and their values
my %Cline;
# Hash of control lines indicating which ones are low
my %Cislow;
# Hash of previously seen opcodes
my %Opcode;
# Bitmask to set all active low lines off
my $lowoffmask=0;
# The ROM to write out
my @ROM;
# The current instruction, name and microsequence position.
# Also, the number of instructions bytes
my $opcode=0;
my $opname;
my $seq=0;
my $instrlen=0;
# If set, this line is placed at position 0 in each microsequence
my $startline;
# Debug flag if needed. Current line number
my $debug=0;
my $linenum=0;
# Given a string with space-separated control lines, parse each
# word, and update the ROM with the microinstruction
sub parse_lines {
my ($input, $inscode, $insname)= @_;
print("Parsing $input\n") if ($debug==2);
# Start with all lines off
my $result= $lowoffmask;
foreach my $name (split(m{\s+}, $input)) {
die("Unrecognised line $name on line $linenum\n")
if (!defined($Cline{$name}));
# Set the named line in the result
printf(" %s: %04x => ", $name, $result) if ($debug==2);
if ($Cislow{$name}) {
$result ^= $Cline{$name};
} else {
$result |= $Cline{$name};
}
printf("%04x\n", $result) if ($debug==2);
# Increment the instruction length if we have a PCincr
$instrlen++ if ($name eq "PCincr");
print("len $instrlen as PCincr\n") if (($debug==2) && $name eq "PCincr");
}
# Set the ROM for opcode/sequence with this value and
# move to the next sequence position
$ROM[ ($opcode << 4) | $seq ] = $result; $seq++;
printf("%04x: %s\t\t%s %s\n", $result, $input, $inscode, $insname)
if ($debug==1);
}
#### MAIN PROGRAM ####
# Enable debugging
while ( ( @ARGV >= 1 ) && ( $ARGV[0] ) eq "-d" ) {
$debug++;
shift(@ARGV);
}
# Overwrite the opcodes file
open( my $OPOUT, ">", "opcodes" ) || die("Can't write to opcodes: $!\n");
open(my $IN, "<", "microcode") || die("Can't read microcode: $!\n");
while (<$IN>) {
chomp;
$linenum++;
s{#.*}{}; # Lose comments
s{^\s+}{}; # Lose leading whitespace
s{\s+$}{}; # Lose trailing whitespace
next if (m{^$}); # Ignore empty lines
# Starting microinstruction in a microsequence
if (m{:=\s*(.+)}) {
$startline= $1;
print("Got start line $startline\n") if ($debug==2);
next;
}
# Control line definition
if (m{^(\S+)\s*=\s*(\S+)}) {
my ($name, $val)= ($1, $2);
# Determine if it is active low
# Update the low mask if it is
my $islow=0;
if ($name=~ m{^@(.*)}) {
$name= $1; $islow=1;
$lowoffmask |= hex($val);
}
die("Control line $name redefined on line $linenum\n")
if (defined($Cline{$name}));
$Cline{$name}= hex($val);
$Cislow{$name}= $islow; next;
}
# First line in a microsequence
if (m{^(\S+)\s+(\S+)\s*:\s*(.+)}) {
# Get the new opcode number, name and list of control lines
my $newopcode= $1; my $newopname= $2; my $linelist=$3;
print("$newopcode $newopname:\n") if ($debug==2);
die("Opcode $opcode redefined on line $linenum\n")
if (defined($Opcode{$opcode}));
# Print out the details of the last opcode
printf($OPOUT "%02x %d %s\n", $opcode, $instrlen, $opname) if ($seq!=0);
# Replace with the new information and reset the sequence
$opcode= hex($newopcode); $opname= $newopname; $seq=0; $instrlen=0;
# If there is a start line, parse it first
parse_lines($startline, $newopcode, $newopname) if (defined($startline));
# Parse the control lines
parse_lines($linelist, "", ""); next;
}
# If we get here, the line must just be a list of control lines.
# Parse them
parse_lines($_, "", "");
}
close($IN);
# Print out the details of the last opcode
printf($OPOUT "%02x %d %s\n", $opcode, $instrlen, $opname) if ($seq!=0);
close($OPOUT);
# Find any instructions which were not defined and turn them into a NOP.
# This assumes that instruction 00 is a NOP definition.
my $irloadvalue= $ROM[0];
my $nopvalue= $ROM[1];
foreach my $i (0 .. 0xff) {
if (!defined($ROM[ ($i << 4) ])) {
$ROM[ ($i << 4) ] = $irloadvalue;
$ROM[ ($i << 4) + 1 ] = $nopvalue;
}
}
# Write the ROM out in hex
open( my $OUT, ">", "ucode.rom" ) || die("Can't write to ucode.rom: $!\n");
for my $i ( 0 .. ( 2**12 - 1 ) ) {
printf( $OUT "%x ", $ROM[$i] ? $ROM[$i] : 0 );
print( $OUT "\n" ) if ( ( $i % 16 ) == 15 );
}
close($OUT);
# Write the minipro binary ROM file, little-endian 16-bit values.
# The ROM has 16 bits of addressing, so we have to write 2^16 values.
open($OUT, '>:raw', '27Cucode.rom') or die "Unable to open: $!";
for my $i ( 0 .. ( 2**16 - 1 ) ) {
print($OUT pack("v", $ROM[$i] ? $ROM[$i] : 0 ));
}
close($OUT);
exit(0);