-
Notifications
You must be signed in to change notification settings - Fork 5
/
convert-to-vasm.py
121 lines (85 loc) · 3.19 KB
/
convert-to-vasm.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
#!/usr/bin/env python
#
# ******************************************************************************
#
# CONVERT REPOSITORY ASSEMBLER FORMAT TO VASM
#
# Written by Mark Moxon
#
# This script converts source code from the repository style into
# vasm-compatible ARM assembler.
#
# ******************************************************************************
import re
def convert(input_file, output_file):
for line in input_file:
# Strip comments
line = re.sub(r" *[;\\].*$", "", line)
# .label -> label:
line = re.sub(r"^\.([^ \n]+)", r"\1:", line)
# Remove INT(...)
line = re.sub(r" INT\(", " (", line)
# Remove lines that mention pass%
line = re.sub(r"^.*pass%.*$", "", line)
# Remove lines that start with "DIM "
line = re.sub(r"^ *DIM .*$", "", line)
# Remove lines that start with "O% = "
line = re.sub(r"^ *O% = .*$", "", line)
# Remove lines that start with "OSCLI "
line = re.sub(r"^ *OSCLI .*$", "", line)
# Remove lines that start with [ or ]
line = re.sub(r"^ *(\[|\]).*$", "", line)
# "P% = " -> .org
line = re.sub(r"^ *P% = ", ".org ", line)
# FOR loop -> .rept
line = re.sub(r"^ *FOR I% = 1 TO ", ".rept ", line)
# NEXT -> .endr
line = re.sub(r"^ *NEXT", ".endr", line)
# x = y -> .set x, y
line = re.sub(r"^ *(.+) = (.+)$", r".set \1, \2", line)
# SKIP -> .skip
line = re.sub(r"^ *SKIP ", ".skip ", line)
# P% -> $
line = re.sub(r"P%", "$", line)
# Binary % -> 0x
binary_string = re.search(r"%([0-1]+)", line)
if binary_string:
hex_string = hex(int(binary_string.group(1), 2))
line = re.sub(r"%([0-1]+)", hex_string, line)
# Hexadecimal & -> 0x
line = re.sub(r"&", "0x", line)
# EQUD -> .long
line = re.sub(r"^ *EQUD ", ".long ", line)
# EQUW -> .word
line = re.sub(r"^ *EQUW ", ".word ", line)
# EQUS -> .byte
line = re.sub(r"^ *EQUS ", ".byte ", line)
# EQUB -> .byte
line = re.sub(r"^ *EQUB ", ".byte ", line)
# ALIGN -> .balign
line = re.sub(r"^ *ALIGN", ".balign 4", line)
# IF -> .ifdef
line = re.sub(r"^ *IF ", ".ifdef ", line)
# ENDIF -> .endif
line = re.sub(r"^ *ENDIF", ".endif", line)
# ELSE -> .else
line = re.sub(r"^ *ELSE", ".else", line)
# INCBIN -> .incbin
line = re.sub(r"^ *INCBIN", ".incbin", line)
# INCLUDE -> .include
line = re.sub(r"^ *INCLUDE", ".include", line)
# Write updated line
if line.strip():
output_file.write(line)
print("Converting 1-source-files/Lander.arm")
source_file = open("1-source-files/main-sources/Lander.arm", "r")
vasm_file = open("3-assembled-output/Lander.arm", "w")
convert(source_file, vasm_file)
source_file.close()
vasm_file.close()
source_file = open("1-source-files/main-sources/RunImage.arm", "r")
vasm_file = open("3-assembled-output/RunImage.arm", "w")
convert(source_file, vasm_file)
source_file.close()
vasm_file.close()
print("3-assembled-output/Lander.arm file saved")