-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
traps.py
80 lines (60 loc) · 1.77 KB
/
traps.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
from memory import mem_read, mem_write, Registers, reg_read, reg_write
from utils import sign_extend
from getch import getch
import sys
class Halt(Exception):
"""Thrown to indicate HALT instruction has been executed."""
pass
def _GETC():
"""get character from keyboard,
character is not echoed onto the console. """
ch = getch()
reg_write(Registers.R0, ord(ch))
def _OUT():
"""output a character"""
sys.stdout.write(chr(reg_read(Registers.R0)))
sys.stdout.flush()
def _PUTS():
"""output a word string"""
for i in range(reg_read(Registers.R0), 2**16):
ch = mem_read(i)
if ch == 0: # check if the char is not null then print this char
break
sys.stdout.write(chr(ch))
sys.stdout.flush() # equal to fflush() in c
def _IN():
"""input a single character, echoed onto the console"""
sys.stdout.write("Enter a character: ")
sys.stdout.flush()
reg_write(Registers.R0, ord(sys.stdin.read(1)))
def _PUTSP():
"""output a byte string"""
for i in range(reg_read(Registers.R0), 2**16):
c = mem_read(i)
if c == 0:
break
sys.stdout.write(chr(c & 0xFF))
char = c >> 8
if char:
sys.stdout.write(chr(char))
sys.stdout.flush()
def _HALT():
"""halt the program"""
raise Halt()
class Traps:
GETC = 0x20 # get character from keyboard
OUT = 0x21 # output a character
PUTS = 0x22 # output a word string
IN = 0x23 # input a string
PUTSP = 0x24 # output a byte string
HALT = 0x25 # halt the program
_traps = {
Traps.GETC: _GETC,
Traps.OUT: _OUT,
Traps.PUTS: _PUTS,
Traps.IN: _IN,
Traps.PUTSP: _PUTSP,
Traps.HALT: _HALT
}
def trap_routine(code):
return _traps[code]