-
Notifications
You must be signed in to change notification settings - Fork 5
/
genmc.pl
219 lines (199 loc) · 6.13 KB
/
genmc.pl
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
#!/usr/bin/perl
use strict ;
open(MICRO, ">MICROCODE.h") or die() ;
my %INSTS = (
'0000' => {
'name' => 'LD',
'4e' => 'RegA_e',
'4s' => 'MAR_s',
'5e' => 'RAM_e',
'5s' => 'RegB_s',
},
'0001' => {
'name' => 'ST',
'4e' => 'RegA_e',
'4s' => 'MAR_s',
'5e' => 'RegB_e',
'5s' => 'RAM_s',
},
'0010' => {
'name' => 'DATA',
'4e' => 'BUS1|IAR_e',
'4s' => 'MAR_s|ACC_s',
'5e' => 'RAM_e',
'5s' => 'RegB_s',
'6e' => 'ACC_e',
'6s' => 'IAR_s',
skip => sub { if ($_[0] > 3) { return "Invalid instruction"} ; return undef ;},
},
'0011' => {
'name' => 'JMPR',
'4e' => 'RegB_e',
'4s' => 'IAR_s',
skip => sub { if ($_[0] > 3) { return "Invalid instruction"} ; return undef ;},
},
'0101' => {
'name' => 'JCOND',
'4e' => 'BUS1|IAR_e',
'4s' => 'MAR_s|ACC_s',
'5e' => 'ACC_e',
'5s' => 'IAR_s',
'6e' => 'RAM_e',
'6s' => 'JCOND',
},
'0100' => {
'name' => 'JMP',
'4e' => 'IAR_e',
'4s' => 'MAR_s',
'5e' => 'RAM_e',
'5s' => 'IAR_s',
skip => sub { if ($_[0] > 0) { return "Invalid instruction"} ; return undef ;},
},
'01100' => {
'name' => 'CLF',
'4e' => 'BUS1',
'4s' => 'FLAGS_s',
skip => sub { if ($_[0] > 0) { return "Invalid instruction"} ; return undef ;},
},
'01101' => {
'name' => 'HLT',
'4e' => 'HLT',
skip => sub { if ($_[0] > 0) { return "Invalid instruction"} ; return undef ;},
},
'011100' => {
'name' => 'IO',
'4e' => '',
'4s' => '',
'5e' => 'IO_e',
'5s' => 'RegB_s',
},
'011101' => {
'name' => 'IO',
'4e' => 'IO_da',
'4s' => 'IO_da',
'5e' => 'IO_e|IO_da',
'5s' => 'RegB_s|IO_da',
},
'011110' => {
'name' => 'IO',
'4e' => 'RegB_e|IO_io',
'4s' => 'IO_s|IO_io',
'5e' => 'IO_io',
'5s' => 'IO_io',
},
'011111' => {
'name' => 'IO',
'4e' => 'RegB_e|IO_io|IO_da',
'4s' => 'IO_s|IO_io|IO_da',
'5e' => 'IO_io|IO_da',
'5s' => 'IO_io|IO_da',
},
ALU('1000', 'ADD'),
ALU('1001', 'SHR'),
ALU('1010', 'SHL'),
ALU('1011', 'NOT'),
ALU('1100', 'AND'),
ALU('1101', 'OR'),
ALU('1110', 'XOR'),
ALU('1111', 'CMP'),
) ;
my $now = localtime() ;
print MICRO <<CPP ;
/*
* Microcode dispatch table.
* This file was generated on $now by $0.
*/
#ifndef MICROCODE_h
#define MICROCODE_h
#define HLT 0b10000000000000000000000000000000
#define MAR_s 0b01000000000000000000000000000000
#define RAM_e 0b00100000000000000000000000000000
#define RAM_s 0b00010000000000000000000000000000
// #define X 0b00001000000000000000000000000000
#define ALU2 0b00000100000000000000000000000000
#define ALU1 0b00000010000000000000000000000000
#define ALU0 0b00000001000000000000000000000000
#define TMP_s 0b00000000100000000000000000000000
#define ACC_e 0b00000000010000000000000000000000
#define ACC_s 0b00000000001000000000000000000000
#define BUS1 0b00000000000100000000000000000000
#define FLAGS_s 0b00000000000010000000000000000000
#define Ctmp_e 0b00000000000001000000000000000000
#define IAR_e 0b00000000000000100000000000000000
// #define X 0b00000000000000010000000000000000
#define IO_s 0b00000000000000001000000000000000
#define IO_e 0b00000000000000000100000000000000
#define IO_io 0b00000000000000000010000000000000
#define IO_da 0b00000000000000000001000000000000
#define R0_e 0b00000000000000000000100000000000
#define R1_e 0b00000000000000000000010000000000
#define R0_s 0b00000000000000000000001000000000
#define R1_s 0b00000000000000000000000100000000
// #define X 0b00000000000000000000000010000000
#define JCOND 0b00000000000000000000000001000000
#define IAR_s 0b00000000000000000000000000100000
#define R2_e 0b00000000000000000000000000010000
#define R3_e 0b00000000000000000000000000001000
#define R2_s 0b00000000000000000000000000000100
#define R3_s 0b00000000000000000000000000000010
#define IR_s 0b00000000000000000000000000000001
CPP
my $n = 0 ;
print MICRO "PROGMEM const unsigned long microcode[] = {\n" ;
foreach my $inst (sort keys %INSTS){
my $arg_len = 8 - length($inst) ;
my $arg_max = (2 ** $arg_len) - 1 ;
foreach my $arg (0 .. $arg_max){
foreach my $step (4, 5, 6){
foreach my $clk ('e', 's'){
my $binarg = sprintf("%0*b", $arg_len, $arg) ;
my $bits = $INSTS{$inst}->{"$step$clk"} || '0' ;
my $info = '' ;
if ($INSTS{$inst}->{skip}){
my $msg = $INSTS{$inst}->{skip}->($arg) ;
if ($msg){
$info = " ($msg)" ;
$bits = 0 ;
}
}
$bits =~ s/RegA(_[es])/RegAB('A', $arg) . "$1"/e ;
$bits =~ s/RegB(_[es])/RegAB('B', $arg) . "$1"/e ;
printf MICRO " /* Entry %4d (inst*6+((step-4)*2)+clk) -> %5s:$inst$binarg\@$step$clk $info */ ", $n, $INSTS{$inst}->{name} ;
print MICRO "$bits,\n" ;
$n++ ;
}
}
}
}
print MICRO "} ;\n\n#endif" ;
sub ALU {
my $code = shift ;
my $name = shift ;
my $bin = oct("0b$code") ;
my $ALU = '' ;
$ALU .= "ALU2|" if $bin & 0b100 ;
$ALU .= "ALU1|" if $bin & 0b010 ;
$ALU .= "ALU0|" if $bin & 0b001 ;
return (
$code => {
'name' => $name,
'4e' => $ALU . 'RegB_e',
'4s' => $ALU . 'TMP_s',
'5e' => $ALU . 'RegA_e|Ctmp_e',
'5s' => $ALU . 'ACC_s|FLAGS_s',
'6e' => $ALU . 'ACC_e',
'6s' => ($code == '1111' ? '' : 'RegB_s'),
}
) ;
}
sub RegAB {
my $reg = shift ;
my $arg = shift ;
if ($reg eq 'B'){
$arg = $arg & 0b0011 ;
}
if ($reg eq 'A'){
$arg = $arg >> 2 ;
}
return "R$arg" ;
}