-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassembler.py
executable file
·180 lines (178 loc) · 6.18 KB
/
assembler.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
#!/usr/bin/python3
#This is an simple assembler for the asm code for the 4 bit processor
###############################################
#The instructions are:
#NOP 0x00 - do nothing
#
#TOG 0x1X - toggle
# ROM 0 - ROM data bus connection
# ALURES 1 - ALU result bus connection
# RAMWR 2 - RAM write/read
# RSEL 3 - select RAM
#
#WRI 0x2X - write to...
# PCOU 0 - program counter
# PARA 1 - program address register A
# PARB 2 - program address register B
# PARC 3 - program address register C
# ALUAR 4 - ALU A register
# ALUBR 5 - ALU B register
# ALUOR 6 - ALU operation register
# ALU operations:
# 0 - logic AND
# 1 - logic OR
# 2 - logic NOR
# 3 - logic XOR
# 4 - add
# F - sub
# ALURR 7 - ALU result register
# RAMADDR 8 - RAM address register
# PCOUZ 9 - program counter if zero flag
# PCOUNZ A - program counter if no zero flag
# PCOUC B - program counter if carry flag
# PCOUNZ C - program counter if no zero flag
#
#WRIDAT X 0x3X - write 4 bits of data to ROM register
#
#DJMP XXX 0x4X
# 0xXX - direct jump to a 12 bit ROM address, MSB first LSB last
#DJMPZF XXX 0x5X
# 0xXX - direct jump on zero flag set
#DJMPCF XXX 0x6X
# 0xXX - direct jump on carry flag
###############################################
#Line starting with '#' will be ignored
#Every word that's not an instruction fron the list abowe will be ignored
#
#You can put labels in code with '&LABEL'
#Then you can direct jump to those labels
#Instructions below the label will be executed
#example:
#&LABEL
#DJMP &LABEL
###############################################
import sys
if len(sys.argv) < 3:
print('no path to input or output file')
print('use: program [input_file] [output_file]')
sys.exit(1)
elif len(sys.argv) > 3:
print('too many arguments')
print('use: program [input_file] [output_file]')
sys.exit(1)
machine_code_fp = open(sys.argv[2], 'w')
labels = {}
machine_code = []
machine_code.append('v2.0 raw')
asm_fp = open(sys.argv[1], 'r')
lines_list = asm_fp.readlines()
for line in lines_list:
if line[0] == '#':
continue
elif line[0] == '&':
labels[line.split()[0]] = len(machine_code) - 1
else:
if 'NOP' in line:
machine_code.append('00')
continue
elif 'TOG ' in line:
if 'ROM' in line:
machine_code.append('10')
continue
elif 'ALURES' in line:
machine_code.append('11')
continue
elif 'RAMWR' in line:
machine_code.append('12')
continue
elif 'RSEL' in line:
machine_code.append('13')
continue
else:
sys.exit('unknown TOG argument in line ' \
+ lines_list.index(line) + ': ' + line)
elif 'WRI ' in line:
if 'PCOU' in line:
machine_code.append('20')
continue
elif 'PARA' in line:
machine_code.append('21')
continue
elif 'PARB' in line:
machine_code.append('22')
continue
elif 'PARC' in line:
machine_code.append('23')
continue
elif 'ALUAR' in line:
machine_code.append('24')
continue
elif 'ALUBR' in line:
machine_code.append('25')
continue
elif 'ALUOR' in line:
machine_code.append('26')
continue
elif 'ALURR' in line:
machine_code.append('27')
continue
elif 'RAMADDR' in line:
machine_code.append('28')
continue
elif 'PCOUZ' in line:
machine_code.append('29')
continue
elif 'PCOUNZ' in line:
machine_code.append('2a')
continue
elif 'PCOUC' in line:
machine_code.append('2b')
continue
elif 'PCOUNZ' in line:
machine_code.append('2c')
continue
else:
sys.exit('unknown WRI argument in line ' \
+ lines_list.index(line) + ': ' + line)
elif 'WRIDAT ' in line:
machine_code.append('3' + line.split()[1] + '')
continue
elif 'DJMP ' in line:
if '&' in line:
machine_code.append('4' + ' ' + line.split()[1])
machine_code.append('place_holder')
continue
else:
machine_code.append('4' + list(line.split()[1])[0])
machine_code.append(list(line.split()[1])[1] + \
list(line.split()[1])[2])
continue
elif 'DJMPZF ' in line:
if '&' in line:
machine_code.append('5' + ' ' + line.split()[1])
machine_code.append('place_holder')
continue
else:
machine_code.append('5' + list(line.split()[1])[0])
machine_code.append(list(line.split()[1])[1] + \
list(line.split()[1])[2])
continue
elif 'DJMPCF ' in line:
if '&' in line:
machine_code.append('6' + ' ' + line.split()[1])
machine_code.append('place_holder')
continue
else:
machine_code.append('6' + list(line.split()[1])[0])
machine_code.append(list(line.split()[1])[1] + \
list(line.split()[1])[2])
continue
for command in machine_code:
if '&' in command:
hexaddress_str = "%0.3X" % labels[command.split()[1]]
index_of_label = machine_code.index(command)
machine_code[index_of_label] = command[0] \
+ hexaddress_str[0]
machine_code[index_of_label + 1] = hexaddress_str[1:3]
for line in machine_code:
machine_code_fp.write(line + '\r\n')