-
Notifications
You must be signed in to change notification settings - Fork 0
/
f4_misc.py
518 lines (395 loc) · 14 KB
/
f4_misc.py
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
"""
F-4 MISC 16 bit implementation
instruction opcode operand operation clocks
ADDi imm 00 01 16 bit value imm+(A) --> A 3
ADDm addr 00 02 16 bit address (addr)+(A) --> A 4
ADDpc 00 04 null operand PC+(A) --> A 3
BVS addr 00 08 16 bit address (addr) --> PC if <v>=1 3
LDAi imm 00 10 16 bit value imm --> A 3
LDAm addr 00 20 16 bit address (addr) --> A 3
LDApc 00 40 null operand PC --> A 3
STAm addr 00 80 16 bit address A --> (addr) 3
STApc PC 01 00 null operand A --> PC 3
References:
http://www.dakeng.com/misc.html
"""
from dataclasses import asdict, field, dataclass
import functools
import logging
from typing import (
Any,
Callable,
ClassVar,
Generator,
MutableMapping,
Optional,
Self,
Sequence,
Tuple,
)
import vm_types
import virtual_machine
from toy_assembler import Assembler
logger = logging.getLogger(__name__)
HALT_INS_CODE = 0xFFFF
WORD_SIZE = 16
class InstructionCodes(vm_types.GenericInstructionSet):
ADDi = 1
ADDm = 2
ADDpc = 4
BVS = 8
LDAi = 10
LDAm = 20
LDApc = 40
STAm = 80
STApc = 100
HALT = HALT_INS_CODE
@dataclass
class InlineParamToken:
value: str
def encode(
self, assembler_instance: vm_types.GenericAssembler, offset: int
) -> bytes | bytearray:
word_size_bytes = assembler_instance.word_size_bytes
signed = False
match self.value[:2]:
case "0x":
base = 16
case "0b":
base = 2
case _:
base = 10
if self.value.strip().startswith("-"):
signed = True
return int(self.value, base).to_bytes(signed=signed, length=word_size_bytes)
@dataclass
class LabelParamToken:
value: str
def encode(self, assembler_instance: vm_types.GenericAssembler, offset: int):
word_size_bytes = assembler_instance.word_size_bytes
assembler_instance.symbol_table["refs"].setdefault(self.value, []).append(
len(assembler_instance.byte_code) + offset
)
return bytes(word_size_bytes)
@dataclass
class LabelOrInlineParamToken:
value: str
def encode(self, assembler_instance: vm_types.GenericAssembler, offset: int):
if self.value.startswith("."):
return LabelParamToken(value=self.value)
else:
return InlineParamToken(value=self.value)
class MultiInlineParamToken(InlineParamToken):
value_list: Sequence[str]
value: str
def encode(self, assembler_instance: vm_types.GenericAssembler, offset: int):
byte_code = bytearray()
self.value_list = self.value.split(",")
for str_val in self.value_list:
self.value = str_val.strip()
byte_val = super().encode(assembler_instance, offset)
byte_code.extend(byte_val)
return byte_code
@dataclass
class InstructionToken:
code: InstructionCodes
params: Sequence[vm_types.AssemblerParamToken]
def encode(self, assembler_instance: vm_types.GenericAssembler) -> bytes:
byte_code = bytearray()
inst_bytes = self.code.value.to_bytes(
length=assembler_instance.word_size_bytes,
)
byte_code.extend(inst_bytes)
for param_token in self.params:
bytes_or_token = param_token
while not (
isinstance(bytes_or_token, bytes)
or isinstance(bytes_or_token, bytearray)
):
# TODO: ADD MAX DEPTH ERROR
bytes_or_token = bytes_or_token.encode(
assembler_instance=assembler_instance, offset=len(byte_code)
)
byte_code.extend(bytes_or_token)
return bytes(byte_code)
@dataclass
class InstructionMeta:
ins: type[vm_types.AssemblerToken] = InstructionToken
params: Sequence[type[vm_types.AssemblerParamToken]] = field(default_factory=list)
GenericOneLabelOrInlineParamIns = InstructionMeta(params=[LabelOrInlineParamToken])
class InstructionsMeta:
def __getitem__(self, key):
try:
inst = InstructionCodes(key)
if inst in (
InstructionCodes.HALT,
InstructionCodes.LDApc,
InstructionCodes.STApc,
):
return InstructionMeta()
return GenericOneLabelOrInlineParamIns
except ValueError:
raise ValueError(f"Unrecognized instruction with mnemonic {key}")
@dataclass
class MacroMeta:
params: Sequence[type[vm_types.AssemblerParamToken]] = field(default_factory=list)
@dataclass
class SetLabelParamToken:
value: str
def encode(
self, assembler_instance: vm_types.GenericAssembler, offset: int
) -> bytes:
return b""
@dataclass
class SetLabelToken:
params: Sequence[vm_types.AssemblerParamToken]
def encode(self, assembler_instance: vm_types.GenericAssembler) -> None:
assembler_instance.symbol_table["map"][self.params[0].value] = len(
assembler_instance.byte_code
)
@dataclass
class DefineWordToken:
params: Sequence[vm_types.AssemblerParamToken]
def encode(self, assembler_instance: vm_types.GenericAssembler) -> bytes:
byte_code = bytearray()
for param_token in self.params:
bytes_or_token = param_token
while not (
isinstance(bytes_or_token, bytes)
or isinstance(bytes_or_token, bytearray)
):
# TODO: ADD MAX DEPTH ERROR
bytes_or_token = bytes_or_token.encode(
assembler_instance=assembler_instance, offset=len(byte_code)
)
byte_code.extend(bytes_or_token)
return bytes(byte_code)
MACROS_META = {
"LABEL": (SetLabelToken, MacroMeta(params=[SetLabelParamToken])),
"DWORD": (DefineWordToken, MacroMeta(params=[MultiInlineParamToken])),
}
TEST_PROG = """
# F-4 16-bit word tests
# Code examples from source listed in module comments
LABEL .START:
LDAi .FIRST_JUMP
STApc
LABEL .NUMBER:
DWORD 0b1000_0000_0000_0001,0xFFFF
LABEL .FIRST_JUMP:
# SHL (arithmetic shift left) is a quick addition of a number to itself.
# The overflow flag is set if the bit shifted out is 1, clear if it's 0.
LDAm .NUMBER
ADDm .NUMBER
STAm .NUMBER
# Branch overflow on first add, exit second
LDAi 0xFFFF
LABEL .BRANCH_OV:
ADDi 1
BVS .BRANCH_OV
HALT
"""
@dataclass
class AddressModes:
IMMEDIATE = 0
DIRECT = 1
REGISTER = 2
@dataclass
class CPUFlags:
overflow: bool = False
class CPURegisterNames(vm_types.GenericCPURegisterNames):
PC = "PC"
A = "A"
IC = "IC"
HIDDEN = "HIDDEN"
@dataclass
@CPURegisterNames.validate_register_names
class CPURegisters:
PC: int = 0
A: int = 0
IC: int = 0
HIDDEN: int = 0
@dataclass
class CentralProcessingUnit:
HALT_INS_CODE: int
RAM: memoryview
FLAGS: CPUFlags
REGISTERS: CPURegisters = field(default_factory=CPURegisters)
WORD_SIZE_BITS: int = WORD_SIZE
INSTRUCTION_MAP: ClassVar[
MutableMapping[InstructionCodes, Callable[[Self], None]]
] = {}
@classmethod
def register_instruction(cls, inst_code) -> vm_types.DecoratorCallable:
def decorator(f) -> Callable:
cls.INSTRUCTION_MAP[inst_code] = f
return f
return decorator
@property
def word_in_bytes(self) -> int:
return self.WORD_SIZE_BITS // vm_types.BITS_IN_BYTE
@property
def nible_in_bytes(self) -> int:
return self.word_in_bytes // 2
def fetch(self):
next_ip = self.REGISTERS.PC + self.word_in_bytes
self.REGISTERS.IC = int.from_bytes(self.RAM[self.REGISTERS.PC : next_ip])
self.REGISTERS.PC = next_ip
def reset(self):
self.REGISTERS = CPURegisters()
self.FLAGS = CPUFlags()
def exec(self):
inst_code = InstructionCodes(self.REGISTERS.IC)
logger.debug(f"Instruction: {inst_code}")
inst_func = self.INSTRUCTION_MAP[inst_code]
inst_func(self)
def run(self) -> Generator:
while not self.fetch() and self.REGISTERS.IC != self.HALT_INS_CODE:
yield
logger.debug("Running CPU step ...")
self.exec()
logger.debug(f"{self.REGISTERS=}")
logger.debug(f"{self.FLAGS=}")
def dump_registers(self):
return asdict(self.REGISTERS)
@property
def current_inst_address(self) -> int:
return self.REGISTERS.PC
@property
def current_stack_address(self) -> int:
return len(self.RAM)
@current_stack_address.setter
def current_stack_address(self, address: int) -> int:
return self.current_stack_address
@classmethod
def bind_to_registers(
cls, bind_list: Sequence[Tuple[InstructionCodes, dict[str, Any]]]
) -> vm_types.DecoratorCallable:
def decorator(f: Callable) -> Callable:
for ins_code, kwargs in bind_list:
bound_func = functools.partial(f, **kwargs)
cls.register_instruction(ins_code)(bound_func)
return f
return decorator
def load(
instance: CentralProcessingUnit,
reg_name: str,
*,
source_reg_name: Optional[str] = None,
ip_unmodified=False,
address_mode=AddressModes.IMMEDIATE,
):
bytes_val = instance.RAM[
instance.REGISTERS.PC : instance.REGISTERS.PC + instance.word_in_bytes
]
val = int.from_bytes(bytes_val)
match address_mode:
case AddressModes.IMMEDIATE:
pass
case AddressModes.DIRECT:
address = val
val = int.from_bytes(
instance.RAM[address : address + instance.word_in_bytes]
)
case AddressModes.REGISTER:
if not source_reg_name:
raise ValueError("Missing source register name")
val = getattr(instance.REGISTERS, source_reg_name)
ip_unmodified = True
case _:
raise ValueError(f"Unknown {address_mode=}")
setattr(instance.REGISTERS, reg_name, val)
if not ip_unmodified:
instance.REGISTERS.PC += instance.word_in_bytes
def store(
instance: CentralProcessingUnit,
source_reg_name: str,
*,
dest_reg_name: Optional[str] = None,
address_mode=AddressModes.IMMEDIATE,
):
val = getattr(instance.REGISTERS, source_reg_name)
match address_mode:
case AddressModes.DIRECT:
val = val.to_bytes(length=instance.word_in_bytes)
bytes_val = instance.RAM[
instance.REGISTERS.PC : instance.REGISTERS.PC + instance.word_in_bytes
]
address = int.from_bytes(bytes_val)
for idx in range(instance.word_in_bytes):
instance.RAM[address + idx] = val[idx]
instance.REGISTERS.PC += instance.word_in_bytes
case AddressModes.REGISTER:
if not dest_reg_name:
raise ValueError("Missing destination register name")
setattr(instance.REGISTERS, dest_reg_name, val)
case _:
raise ValueError(f"Unknown {address_mode=}")
def add(
instance: CentralProcessingUnit,
source_reg_name: Optional[str] = None,
*,
address_mode=AddressModes.IMMEDIATE,
):
instance.FLAGS.overflow = False
load(instance, "HIDDEN", source_reg_name=source_reg_name, address_mode=address_mode)
val = instance.REGISTERS.HIDDEN + instance.REGISTERS.A
instance.REGISTERS.A = val & 0xFFFF
instance.FLAGS.overflow = bool((val & 0x010000))
@CentralProcessingUnit.register_instruction(InstructionCodes.LDAi)
def loada_i(instance: CentralProcessingUnit):
load(instance, "A")
@CentralProcessingUnit.register_instruction(InstructionCodes.LDAm)
def loada_m(instance: CentralProcessingUnit):
load(instance, "A", address_mode=AddressModes.DIRECT)
@CentralProcessingUnit.register_instruction(InstructionCodes.LDApc)
def loada_pc(instance: CentralProcessingUnit):
load(instance, "A", source_reg_name="PC", address_mode=AddressModes.REGISTER)
@CentralProcessingUnit.register_instruction(InstructionCodes.STAm)
def staa_m(instance: CentralProcessingUnit):
store(instance, source_reg_name="A", address_mode=AddressModes.DIRECT)
@CentralProcessingUnit.register_instruction(InstructionCodes.STApc)
def staa_pc(instance: CentralProcessingUnit):
store(
instance,
source_reg_name="A",
dest_reg_name="PC",
address_mode=AddressModes.REGISTER,
)
@CentralProcessingUnit.register_instruction(InstructionCodes.BVS)
def branch_if_overflow(instance: CentralProcessingUnit):
if instance.FLAGS.overflow:
load(instance, "PC", ip_unmodified=True)
else:
instance.REGISTERS.PC += instance.word_in_bytes
@CentralProcessingUnit.register_instruction(InstructionCodes.ADDi)
def add_inst(instance: CentralProcessingUnit):
add(instance, address_mode=AddressModes.IMMEDIATE)
@CentralProcessingUnit.register_instruction(InstructionCodes.ADDm)
def add_m(instance: CentralProcessingUnit):
add(instance, address_mode=AddressModes.DIRECT)
@CentralProcessingUnit.register_instruction(InstructionCodes.ADDpc)
def add_pc(instance: CentralProcessingUnit):
add(instance, source_reg_name="PC", address_mode=AddressModes.DIRECT)
def instance_factory() -> vm_types.GenericVirtualMachine:
assembler_instance = Assembler(
TEST_PROG, InstructionCodes, WORD_SIZE, InstructionsMeta(), MACROS_META
)
memory = bytearray(4 * 1024)
ram = memoryview(memory)
cpu_instance = CentralProcessingUnit(
HALT_INS_CODE=HALT_INS_CODE,
RAM=ram,
FLAGS=CPUFlags(),
)
vm_instance = virtual_machine.VirtualMachine(
memory=ram, cpu=cpu_instance, assembler=assembler_instance
)
vm_instance.load_program_at(0, TEST_PROG)
vm_instance.restart()
return vm_instance
if __name__ == "__main__":
import log
log.init_logging()
vm_instance = instance_factory()
vm_instance.run()