-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassemble68k.c
266 lines (241 loc) · 4.57 KB
/
assemble68k.c
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
#include <files.h>
#include "assemble68k.h"
file_bits current_file;
dictionary labels;
void file_error(){}
void write_byte(unsigned char byte, FILES *fsp){
unsigned short status;
status = FPutC(byte, fsp);
if(status != FS_OK){
file_error();
}
}
void write_bit(unsigned char bit){
current_file.current_byte = current_file.current_byte>>1 | bit;
if(current_file.current_bitnum == 7){
write_byte(current_file.current_byte, current_file.fsp);
current_file.current_bitnum = 0;
} else {
current_file.current_bitnum++;
}
}
void write_bits(unsigned char byte, unsigned char num_bits){
unsigned char i;
byte <<= (8 - num_bits);
for(i = 0; i < num_bits; i++){
write_bit(byte&0b10000000);
byte <<= 1;
}
}
void write_long(uint32_t l){
unsigned char byte;
byte = l>>24;
write_byte(byte, current_file);
byte = (l>>16)&0b11111111;
write_byte(byte, current_file);
byte = (l>>8)&0b11111111;
write_byte(byte, current_file);
byte = l&0b11111111;
write_byte(byte, current_file);
}
void write_word(uint16_t w){
unsigned char byte;
byte = w>>8;
write_byte(byte, current_file);
byte = w&0b11111111;
write_byte(byte, current_file);
}
unsigned int get_file_pointer(){
return FGetPos(current_file.fsp);
}
label_entry *create_label_entry(unsigned int file_pointer){
label_entry *output;
output = malloc(sizeof(label_entry));
output->found = 0;
output->file_pointer = file_pointer;
return output;
}
void write_size1(SIZE size){
switch(size){
case BYTE:
write_bits(0, 2);
break;
case WORD:
write_bits(1, 2);
break;
case LONG:
write_bits(2, 2);
}
}
void write_size2(SIZE size){
switch(size){
case WORD:
write_bits(0, 1);
break;
case LONG:
write_bits(1, 1);
break;
}
}
void write_size3(SIZE size){
switch(size){
case BYTE:
write_bits(1, 2);
break;
case WORD:
write_bits(3, 2);
break;
case LONG:
write_bits(2, 2);
}
}
void write_mode1(ADDRESS_MODE mode, unsigned int reg){
switch(mode){
case PC_DISP:
write_bits(2, 3);
break;
case PC_IDX:
write_bits(3, 3);
break;
case ABS_SHORT:
write_bits(0, 3);
break;
case ABS_LONG:
write_bits(1, 3);
break;
case IMMEDIATE:
write_bits(4, 3);
break;
default:
write_bits(reg, 3);
}
switch(mode){
case DATA_REG:
write_bits(0, 3);
break;
case ADDRESS_REG:
write_bits(1, 3);
break;
case ADDRESS:
write_bits(2, 3);
break;
case ADDRESS_PI:
write_bits(3, 3);
break;
case ADDRESS_PD:
write_bits(4, 3);
break;
case ADDRESS_DISP:
write_bits(5, 3);
break;
case ADDRESS_IDX:
write_bits(6, 3);
break;
default:
write_bits(7, 3);
}
}
void write_mode2(ADDRESS_MODE mode, unsigned int reg){
switch(mode){
case DATA_REG:
write_bits(0, 3);
break;
case ADDRESS_REG:
write_bits(1, 3);
break;
case ADDRESS:
write_bits(2, 3);
break;
case ADDRESS_PI:
write_bits(3, 3);
break;
case ADDRESS_PD:
write_bits(4, 3);
break;
case ADDRESS_DISP:
write_bits(5, 3);
break;
case ADDRESS_IDX:
write_bits(6, 3);
break;
default:
write_bits(7, 3);
}
switch(mode){
case PC_DISP:
write_bits(2, 3);
break;
case PC_IDX:
write_bits(3, 3);
break;
case ABS_SHORT:
write_bits(0, 3);
break;
case ABS_LONG:
write_bits(1, 3);
break;
case IMMEDIATE:
write_bits(4, 3);
break;
default:
write_bits(reg, 3);
}
}
void write_immediate(unsigned long int immediate, SIZE size){
switch(size){
case BYTE:
write_bits(immediate, 8);
break;
case WORD:
write_word(immediate);
break;
case LONG:
write_long(immediate);
}
}
void write_operation(operation o){
switch(o.op){
case ORI:
write_bits(0, 8);
write_size1(o.size);
write_mode2(o.mode1, o.reg1);
write_immediate(o.immediate, o.size);
break;
case ANDI:
write_bits(2, 8);
write_size1(o.size);
write_mode2(o.mode1, o.reg1);
write_immediate(o.immediate, o.size);
break;
case SUBI:
write_bits(4, 8);
write_size1(o.size);
write_mode2(o.mode1, o.reg1);
write_immediate(o.immediate, o.size);
break;
case ADDI:
write_bits(6, 8);
write_size1(o.size);
write_mode2(o.mode1, o.reg1);
write_immediate(o.immediate, o.size);
break;
case EORI:
write_bits(10, 8);
write_size1(o.size);
write_mode2(o.mode1, o.reg1);
write_immediate(o.immediate, o.size);
break;
case CMPI:
write_bits(12, 8);
write_size1(o.size);
write_mode2(o.mode1, o.reg1);
write_immediate(o.immedaite, o.size);
break;
case BTST:
write_bits(8, 8);
write_bits(0, 2);
write_mode2(o.mode1, o.reg1);
write_immediate(o.immediate, BYTE);
break;
}
}