-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmicrocode.cpp
More file actions
189 lines (178 loc) · 6.17 KB
/
microcode.cpp
File metadata and controls
189 lines (178 loc) · 6.17 KB
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
//
// microcode.cpp
// This module keeps assembler instructions generated...
// ...by IntyBASIC in a list ready to be output.
//
// Created by Oscar Toledo on 07/01/16.
// Copyright (c) 2016 Oscar Toledo. All rights reserved.
//
#include <string>
using namespace std;
#include "global.h"
#include "microcode.h"
static const char *opcode_list[] = {
"ADCR", "ADD", "ADD@", "ADDI", "ADDR", "AND", "AND@", "ANDI", "ANDR",
"B", "BC", "BEQ", "BGE", "BGT", "BLE", "BLT", "BMI", "BNC", "BNE", "BPL",
"CALL", "CLRC", "CLRR", "CMP", "CMP@", "CMPI", "CMPR", "COMR", "DECLE", "DECR",
"INCR", "MOVR", "MVI", "MVI@", "MVII", "MVO", "MVO@", "MULT", "NEGR", "NOP",
"PSHR", "PULR", "RRC", "RETURN", "RSWD",
"SARC", "SLL", "SLR", "SUB", "SUB@", "SUBI", "SUBR", "SWAP",
"TSTR", "XOR", "XOR@", "XORI", "XORR",
};
//
// Saves microcode
//
microcode::microcode(enum microcode_style style, int type, int r1, int r2, string prefix, int value, int offset)
{
this->style = style;
this->type = type;
this->r1 = r1;
this->r2 = r2;
this->prefix = prefix;
this->value = value;
this->offset = offset;
}
//
// Get type of microcode
//
int microcode::get_type(void)
{
return this->type;
}
//
// Get register 1
//
int microcode::get_r1(void)
{
return this->r1;
}
//
// Get register 2
//
int microcode::get_r2(void)
{
return this->r2;
}
//
// Get prefix
//
string microcode::get_prefix(void)
{
return this->prefix;
}
//
// Get value
//
int microcode::get_value(void)
{
return this->value;
}
//
// Generate assembler code for a microcode
//
void microcode::dump(void) {
switch (this->style) {
case M_SINGLE: // Single opcode: NOP
asm_output << "\t" << opcode_list[this->type];
break;
case M_R: // Single register: CLRR R0
asm_output << "\t" << opcode_list[this->type] << " R" << this->r1;
break;
case M_RR: // Double register: MOVR R0,R1
asm_output << "\t" << opcode_list[this->type] << " R" << this->r1 << ",";
if (this->r2 == 7)
asm_output << "PC";
else
asm_output << "R" << this->r2;
break;
case M_NR: // Constant: MVII #5,R0
asm_output << "\t" << opcode_list[this->type] << " #";
if (this->prefix == "")
asm_output << (this->value & 0xffff);
else
mangle_label(this->prefix, this->value);
if (this->offset && (this->offset & 0x8000) == 0)
asm_output << "+" << this->offset;
else if ((this->offset & 0x8000) != 0)
asm_output << "-" << (0x10000 - this->offset);
asm_output << ",R" << this->r1;
break;
case M_NNR: // Constant: ADDI #label-label,R0
asm_output << "\t" << opcode_list[this->type] << " #(";
mangle_label(this->prefix, this->value);
asm_output << "-";
mangle_label(this->prefix, this->r2);
if (this->offset && (this->offset & 0x8000) == 0)
asm_output << "+" << this->offset;
else if ((this->offset & 0x8000) != 0)
asm_output << "-" << (0x10000 - this->offset);
asm_output << ") AND $FFFF,R" << this->r1;
// Note how the AND in expression solves a bug when subtraction creates a big negative
// number, triggering an error in as1600
break;
case M_LR: // Label and register: MVI V2,R0
asm_output << "\t" << opcode_list[this->type] << " ";
if (this->prefix == "") {
asm_output << this->value;
} else if (this->value == -1) {
asm_output << this->prefix;
} else {
mangle_label(this->prefix, this->value);
}
if (this->offset)
asm_output << "+" << this->offset;
asm_output << ",R" << this->r1;
break;
case M_RL: // Register and label: MVO R0,V2
asm_output << "\t" << opcode_list[this->type] << " R" << this->r1 << ",";
if (this->prefix == "")
asm_output << this->value;
else if (this->value == -1)
asm_output << this->prefix;
else
mangle_label(this->prefix, this->value);
if (this->offset && (this->offset & 0x8000) == 0)
asm_output << "+" << this->offset;
else if ((this->offset & 0x8000) != 0)
asm_output << "-" << (0x10000 - this->offset);
break;
case M_A: // Address (jumps): CALL CLRSCR
asm_output << "\t" << opcode_list[this->type] << " ";
if (this->prefix == "")
asm_output << "$+" << this->value;
else if (this->value == -1)
asm_output << this->prefix;
else
mangle_label(this->prefix, this->value);
break;
case M_S: // Shifts: SLL R0,1
asm_output << "\t" << opcode_list[this->type] << " R" << this->r1 << "," << this->value;
break;
case M_M: // Multiply (macro): MULT R0,R4,5
asm_output << "\t" << opcode_list[this->type] << " R" << this->r1 << ",R" << this->r2 << "," << this->value;
break;
case M_L: // Label
mangle_label(this->prefix, this->value);
asm_output << ":";
break;
case M_D: // Single word of data
asm_output << "\t" << opcode_list[this->type] << " " << (this->r1 & 0xffff);
break;
case M_D2: // Double word of data
asm_output << "\t" << opcode_list[this->type] << " " << (this->r1 & 0xffff) << "," << (this->r2 & 0xffff);
break;
case M_DL: // Label as data
asm_output << "\t" << opcode_list[this->type] << " ";
if (this->value == -1)
asm_output << this->prefix;
else
mangle_label(this->prefix, this->value);
if (this->offset)
asm_output << "+" << this->offset;
break;
case M_LITERAL: // Literal assembler code
asm_output << this->prefix;
break;
}
asm_output << "\n";
}