-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8_bit_pic.py
More file actions
486 lines (385 loc) · 13.7 KB
/
Copy path8_bit_pic.py
File metadata and controls
486 lines (385 loc) · 13.7 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
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
"""
Microchip PICmicro MCU implementation.
Specifics:
- Harvard architecture - separate program and data memories
- 21-bit program address - up to 2MB program memory (flash)
- 12 to 16-bit instruction bus
- big-endian instruction words
- 12-bit data address - up to 4kB data memory (RAM)
- 8-bit data bus
Regerences:
- https://ww1.microchip.com/downloads/en/DeviceDoc/31029a.pdf
- https://developerhelp.microchip.com/xwiki/bin/view/products/mcu-mpu/8bit-pic/8-bitbl/8bitblis/
- https://infonics.wordpress.com/wp-content/uploads/2015/03/introduction-to-pic-18-microcontrollers1.pdf
- https://en.wikipedia.org/wiki/PIC_instruction_listings#Mid-range_core_devices_(14_bit)
- https://ww1.microchip.com/downloads/en/DeviceDoc/MPLAB%20XC8%20PIC%20Assembler%20User%27s%20Guide%2050002974A.pdf
"""
from dataclasses import dataclass
import logging
import re
from typing import (
Callable,
ClassVar,
Generator,
Mapping,
MutableMapping,
Self,
Sequence,
)
import vm_types
import virtual_machine
logger = logging.getLogger(__name__)
WORD_SIZE = 8
FLASH_SIZE = 2 * 1024 * 1024
RAM_SIZE = 4 * 1024
class InstructionCodes(vm_types.GenericInstructionSet):
NOP = 0x0000
MOVWF = 0x0080
BCF = 0x1000
BSF = 0x1400
GOTO = 0x2800
MOVLW = 0x3000
class LabelToken:
"""Label token."""
def __init__(self, label: str) -> None:
super().__init__()
self.label = label
def __repr__(self) -> str:
return f"Label('{self.label}')"
def encode(self, assembler_instance: vm_types.GenericAssembler) -> bytes: # type: ignore
assembler_instance.symbol_table["map"][self.label] = len(
assembler_instance.byte_code
)
return b""
class ByteOpToken:
"""Byte-oriented file register operation token."""
def __init__(self, inst: str, op: int, f: str, d: str) -> None:
super().__init__()
self.inst = str(inst)
self.op = int(op)
self.f = str(f)
self.d = str(d)
def __repr__(self) -> str:
return f"{self.inst}(f={self.f}, d={self.d})"
def encode(self, assembler_instance: vm_types.GenericAssembler) -> bytes: # type: ignore
f = resolve_int_value(assembler_instance, self.f, 0x7F)
d = resolve_int_value(assembler_instance, self.d, 0x1)
code = self.op | (d << 7) | f
return code.to_bytes(2, "big")
class NOPToken(ByteOpToken):
"""No operation token."""
def __init__(self) -> None:
super().__init__("NOP", 0x0000, "0", "0")
def __repr__(self) -> str:
return f"{self.inst}()"
class MOVWFToken(ByteOpToken):
"""Move W to f token."""
def __init__(self, f: str) -> None:
super().__init__("MOVWF", 0x0080, f, "1")
def __repr__(self) -> str:
return f"{self.inst}(f={self.f})"
class BitOpToken:
"""Bit-oriented file register operation token."""
def __init__(self, inst: str, op: int, f: str, b: str) -> None:
super().__init__()
self.inst = str(inst)
self.op = int(op)
self.f = str(f)
self.b = str(b)
def __repr__(self) -> str:
return f"{self.inst}(f={self.f}, b={self.b})"
def encode(self, assembler_instance: vm_types.GenericAssembler) -> bytes: # type: ignore
f = resolve_int_value(assembler_instance, self.f, 0x7F)
b = resolve_int_value(assembler_instance, self.b, 0x7)
code = self.op | (b << 7) | f
return code.to_bytes(2, "big")
class BSFToken(BitOpToken):
"""Bit set f token."""
def __init__(self, f: str, b: str) -> None:
super().__init__("BSF", 0x1400, f, b)
class BCFToken(BitOpToken):
"""Bit clear f token."""
def __init__(self, f: str, b: str) -> None:
super().__init__("BCF", 0x1000, f, b)
class LiteralOpToken:
"""Literal or control operation token."""
def __init__(self, inst: str, op: int, k: str, mask: int) -> None:
super().__init__()
self.inst = str(inst)
self.op = int(op)
self.k = str(k)
self.mask = int(mask)
def __repr__(self) -> str:
return f"{self.inst}(k={self.k})"
def encode(self, assembler_instance: vm_types.GenericAssembler) -> bytes: # type: ignore
k = resolve_int_value(assembler_instance, self.k, self.mask)
code = self.op | k
return code.to_bytes(2, "big")
class GOTOToken(LiteralOpToken):
"""Goto address token."""
def __init__(self, k: str) -> None:
super().__init__("GOTO", 0x2800, k, 0x7FF)
class MOVLWToken(LiteralOpToken):
"""Move literal to W token."""
def __init__(self, k: str) -> None:
super().__init__("MOVLW", 0x3000, k, 0xFF)
TEST_PROG = """
STATUS equ 03h
TRISA equ 85h
PORTA equ 05h
nop
bsf STATUS,5
movlw 00h
movwf TRISA
bcf STATUS,5
Start: movlw 02h
movwf PORTA
movlw 00h
movwf PORTA
goto Start
"""
class PICAssembler:
instructions_meta = {} # Not in use
macros_meta = {} # Not in use
PSEUDOOP_RE = re.compile(
r"(?P<name>[A-Za-z]\w*)\s+(?P<pseudoop>\w+)\s+(?P<operands>\S+)(\s+?P<comment>;.*)?"
)
OPCODE_RE = re.compile(
r"((?P<label>[A-Za-z]\w*):\s+)?(?P<opcode>\w+)(\s+(?P<operands>\S+))?(\s+?P<comment>;.*)?"
)
def __init__(
self,
program_text,
instruction_codes: type[vm_types.GenericInstructionSet],
word_size,
instructions_meta: Mapping[str, Callable],
macros_meta: Mapping[str, Callable],
):
self.text = program_text
self.codes = instruction_codes
self.word_size = word_size
self.instructions_meta = instructions_meta
self.macros_meta = macros_meta
self.word_size_bytes = word_size // vm_types.BITS_IN_BYTE
self.symbol_table = {"map": {}, "symbols": {}}
self.byte_code = bytearray()
def load_program(self, program_text):
self.text = program_text
def _process_pseudoop(
self, name: str, pseudoop: str, operands: list[str], line_num: int
) -> None:
match pseudoop:
case "EQU":
# Defines symbol value.
if name in self.symbol_table["symbols"]:
raise Exception(
f"Symbol '{pseudoop}' redefinition on line {line_num}."
)
self.symbol_table["symbols"][name] = operands[0]
case "SET":
# Defines or re-defines symbol value.
self.symbol_table["symbols"][name] = operands[0]
case "ERROR":
# Generates a user-defined error.
raise Exception(f"Error {operands[0]}.")
case _:
raise Exception(f"Unexpected pseudoop '{pseudoop}' on line {line_num}.")
def _process_opcode(
self,
label: str,
opcode: str,
operands: list[str],
line_num: int,
tokens: list[vm_types.AssemblerToken],
) -> None:
if label:
tokens.append(LabelToken(label))
match opcode:
case "MOVWF":
tokens.append(MOVWFToken(operands[0]))
case "NOP":
tokens.append(NOPToken())
case "BCF":
tokens.append(BCFToken(operands[0], operands[1]))
case "BSF":
tokens.append(BSFToken(operands[0], operands[1]))
case "GOTO":
tokens.append(GOTOToken(operands[0]))
case "MOVLW":
tokens.append(MOVLWToken(operands[0]))
case _:
raise Exception(f"Unexpected opcode '{opcode}' on line {line_num}.")
def tokenize(self) -> Sequence[vm_types.AssemblerToken]:
tokens: list[vm_types.AssemblerToken] = []
lines = self.text.split("\n")
for i, line in enumerate(lines):
line_num = i + 1
line = line.strip()
if not line:
# Skip empty lines.
pass
elif line[0] == ";":
# Skip comment lines.
pass
elif m := self.PSEUDOOP_RE.match(line):
# Pseudo-op
pseudoop = m.group("pseudoop").upper()
operands = m.group("operands").split(",")
self._process_pseudoop(m.group("name"), pseudoop, operands, line_num)
elif m := self.OPCODE_RE.match(line):
# Opcode
opcode = m.group("opcode").upper()
operands = m.group("operands") or ""
operands = operands.split(",")
self._process_opcode(
m.group("label"), opcode, operands, line_num, tokens
)
else:
raise Exception(f"Syntax error on line {line_num}.")
logger.debug(tokens)
return tokens
def assemble(self):
self.byte_code = bytearray()
for token in self.tokenize():
if code_bytes := token.encode(self):
self.byte_code.extend(code_bytes)
logger.debug(self.byte_code)
logger.debug(self.symbol_table)
return self.byte_code
def link(self):
return self.byte_code
def compile(self):
self.assemble()
self.link()
return self.byte_code
def resolve_int_value(
assembler: vm_types.GenericAssembler, value: str, mask: int
) -> int:
"""Resolve integer referene"""
if value in assembler.symbol_table["symbols"]:
# Symbol reference.
value = assembler.symbol_table["symbols"][value]
elif value in assembler.symbol_table["map"]:
# Address reference.
value = assembler.symbol_table["map"][value]
else:
# Literal.
pass
try:
int_value = int(value)
except ValueError:
# Try to handle hexadecimal literals with "h" suffix.
int_value = int(value.lower().rstrip("h"), 16)
int_value = int(int_value) & mask
return int_value
@dataclass
class PICCentralProcessingUnit:
RAM: memoryview
INSTRUCTION_MAP: ClassVar[
MutableMapping[InstructionCodes, Callable[[Self, int], None]]
] = {}
ic: int = 0
pc: int = 0
wreg: int = 0
@classmethod
def register_instruction(cls, inst_code) -> vm_types.DecoratorCallable:
def decorator(f):
cls.INSTRUCTION_MAP[inst_code] = f
return f
return decorator
@property
def word_in_bytes(self) -> int:
return 2
@property
def nible_in_bytes(self) -> int:
raise NotImplementedError
def fetch(self):
next_ip = self.pc + self.word_in_bytes
self.ic = int.from_bytes(self.RAM[self.pc : next_ip], "big")
self.pc = next_ip
def reset(self):
self.ic = 0
self.pc = 0
self.wreg = 0
def exec(self):
if self.ic & 0xFF80 == 0:
ic = self.ic
elif self.ic & 0xFF80 == 0x0080:
ic = 0x0080
elif self.ic & 0xFF00 == 0:
ic = self.ic & 0xFF00
elif self.ic & 0xF000 == 0x1000:
ic = self.ic & 0xFC00
elif self.ic & 0xF000 == 0x2000:
ic = self.ic & 0xF800
elif self.ic & 0xF000 == 0x3000:
ic = self.ic & 0xFF00
else:
raise Exception(f"Unexpected instruction code 0x{self.ic:02x}.")
ic_key = InstructionCodes(ic)
logger.debug(f"Executing {ic_key.name} (0x{self.ic:04x}))...")
inst_func = self.INSTRUCTION_MAP[ic_key]
inst_func(self, self.ic)
def run(self) -> Generator:
while True:
self.fetch()
yield
self.exec()
def dump_registers(self):
return {
"IC": self.ic,
"PC": self.pc,
"WREG": self.wreg,
}
@property
def current_inst_address(self) -> int:
return self.pc
@property
def current_stack_address(self) -> int:
return 0
@current_stack_address.setter
def current_stack_address(self, address: int) -> int:
return 0
@PICCentralProcessingUnit.register_instruction(InstructionCodes.NOP)
def nop(cpu: PICCentralProcessingUnit, ic: int):
"""No operation."""
pass
@PICCentralProcessingUnit.register_instruction(InstructionCodes.MOVWF)
def movwf(cpu: PICCentralProcessingUnit, ic: int):
"""Move W to f."""
f = ic & 0x007F
cpu.RAM[FLASH_SIZE + f] = cpu.wreg
logger.debug(f"0x{FLASH_SIZE + f:05x}: 0x{cpu.RAM[FLASH_SIZE + f]:02x}")
@PICCentralProcessingUnit.register_instruction(InstructionCodes.BCF)
def bcf(cpu: PICCentralProcessingUnit, ic: int):
"""Bit clear f."""
b = (ic & 0x0380) >> 7
f = ic & 0x007F
cpu.RAM[FLASH_SIZE + f] &= ~(1 << b) & 0xFF
logger.debug(f"0x{FLASH_SIZE + f:05x}: 0x{cpu.RAM[FLASH_SIZE + f]:02x}")
@PICCentralProcessingUnit.register_instruction(InstructionCodes.BSF)
def bsf(cpu: PICCentralProcessingUnit, ic: int):
"""Bit set f."""
b = (ic & 0x0380) >> 7
f = ic & 0x007F
cpu.RAM[FLASH_SIZE + f] |= 1 << b
logger.debug(f"0x{FLASH_SIZE + f:05x}: 0x{cpu.RAM[FLASH_SIZE + f]:02x}")
@PICCentralProcessingUnit.register_instruction(InstructionCodes.GOTO)
def goto(cpu: PICCentralProcessingUnit, ic: int):
"""Goto address."""
k = ic & 0x07FF
cpu.pc = k
@PICCentralProcessingUnit.register_instruction(InstructionCodes.MOVLW)
def movlw(cpu: PICCentralProcessingUnit, ic: int):
"""Move literal to W."""
k = ic & 0x00FF
cpu.wreg = k
def instance_factory() -> vm_types.GenericVirtualMachine:
memory = bytearray(FLASH_SIZE + RAM_SIZE)
ram = memoryview(memory)
cpu = PICCentralProcessingUnit(RAM=ram)
assembler = PICAssembler(TEST_PROG, InstructionCodes, WORD_SIZE, {}, {})
vm = virtual_machine.VirtualMachine(memory=ram, cpu=cpu, assembler=assembler)
vm.load_program_at(0, TEST_PROG)
vm.restart()
return vm