1
0
mirror of https://github.com/catseye/SixtyPical.git synced 2024-06-01 12:41:30 +00:00

--output cmdline argument, serialize() returns an array of bytes.

This commit is contained in:
Chris Pressey 2018-09-06 16:18:41 +01:00
parent 3fd7e52bc7
commit 4bba75857f
8 changed files with 34 additions and 22 deletions

View File

@ -5,6 +5,9 @@ History of SixtyPical
----
* Split TODO off into own file.
* `sixtypical` no longer writes the compiled binary to standard
output. The `--output` command-line argument should be given.
* Many tests pass when `sixtypical` is run with Python 3.
0.16
----

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3
#!/usr/bin/env python
"""Usage: sixtypical [OPTIONS] FILES
@ -79,7 +79,7 @@ def process_input_files(filenames, options):
if options.analyze_only:
return
fh = sys.stdout
fh = open(options.output, 'wb')
if options.output_format == 'raw':
start_addr = 0x0000
@ -111,7 +111,7 @@ def process_input_files(filenames, options):
# If we are outputting a .PRG, we output the load address first.
# We don't use the Emitter for this b/c not part of addr space.
if options.output_format in ('prg', 'c64-basic-prg', 'vic20-basic-prg'):
fh.write(Word(start_addr).serialize(0))
fh.write(bytearray(Word(start_addr).serialize()))
emitter = Emitter(start_addr)
for byte in prelude:
@ -140,6 +140,10 @@ if __name__ == '__main__':
help="The SixtyPical source files to compile."
)
argparser.add_argument(
"--output", "-o", type=str, metavar='FILENAME',
help="File to which generated 6502 code will be written."
)
argparser.add_argument(
"--origin", type=str, default=None,
help="Location in memory where the `main` routine will be "

View File

@ -24,7 +24,7 @@ elif [ "X$arch" = "Xatari2600" ]; then
elif [ "X$arch" = "Xapple2" ]; then
src="$1"
out=/tmp/a-out.bin
bin/sixtypical --traceback --origin=0x2000 --output-format=raw $src > $out || exit 1
bin/sixtypical --traceback --origin=0x2000 --output-format=raw $src --output $out || exit 1
ls -la $out
cp ~/scratchpad/linapple/res/Master.dsk sixtypical.dsk
# TODO: replace HELLO with something that does like
@ -53,7 +53,7 @@ fi
### do it ###
out=/tmp/a-out.prg
bin/sixtypical --traceback --output-format=$output_format $src > $out || exit 1
bin/sixtypical --traceback --output-format=$output_format $src --output $out || exit 1
ls -la $out
$emu $out
rm -f $out

View File

@ -8,6 +8,7 @@ class Emittable(object):
raise NotImplementedError
def serialize(self, addr):
"""Should return an array of unsigned bytes (integers from 0 to 255.)"""
raise NotImplementedError
@ -25,7 +26,7 @@ class Byte(Emittable):
return 1
def serialize(self, addr=None):
return chr(self.value)
return [self.value]
def __repr__(self):
return "%s(%r)" % (self.__class__.__name__, self.value)
@ -43,7 +44,7 @@ class Word(Emittable):
word = self.value
low = word & 255
high = (word >> 8) & 255
return chr(low) + chr(high)
return [low, high]
def __repr__(self):
return "%s(%r)" % (self.__class__.__name__, self.value)
@ -60,9 +61,11 @@ class Table(Emittable):
return self._size
def serialize(self, addr=None):
buf = ''.join([emittable.serialize() for emittable in self.value])
buf = []
for emittable in self.value:
buf.extend(emittable.serialize())
while len(buf) < self.size():
buf += chr(0)
buf.append(0)
return buf
def __repr__(self):
@ -130,7 +133,7 @@ class HighAddressByte(Emittable):
return 1
def serialize(self, addr=None):
return self.label.serialize()[0]
return [self.label.serialize()[0]]
def __repr__(self):
return "%s(%r)" % (self.__class__.__name__, self.label)
@ -145,7 +148,7 @@ class LowAddressByte(Emittable):
return 1
def serialize(self, addr=None):
return self.label.serialize()[1]
return [self.label.serialize()[1]]
def __repr__(self):
return "%s(%r)" % (self.__class__.__name__, self.label)
@ -167,10 +170,11 @@ class Emitter(object):
self.addr += thing.size()
def serialize(self, stream):
"""`stream` should be a file opened in binary mode."""
addr = self.start_addr
for emittable in self.accum:
chunk = emittable.serialize(addr)
stream.write(chunk)
stream.write(bytearray(chunk))
addr += len(chunk)
def make_label(self, name=None):

View File

@ -20,7 +20,7 @@ class Implied(AddressingMode):
return 0
def serialize(self, addr=None):
return ''
return []
def __repr__(self):
return "%s()" % (self.__class__.__name__)
@ -109,10 +109,9 @@ class Instruction(Emittable):
return 1 + self.operand.size() if self.operand else 0
def serialize(self, addr=None):
return (
chr(self.opcodes[self.operand.__class__]) +
self.operand.serialize(addr)
)
serialized_operand = self.operand.serialize(addr)
assert isinstance(serialized_operand, list), self.operand.__class__
return [self.opcodes[self.operand.__class__]] + serialized_operand
def __repr__(self):
return "%s(%r)" % (self.__class__.__name__, self.operand)

View File

@ -2276,7 +2276,7 @@ Can't `copy` to a memory location that doesn't appear in (outputs trashes).
a, z, and n are trashed, and must be declared as such.
(Note, both n and z are forbidden writes in this tests.)
(Note, both n and z are forbidden writes in this test.)
| byte lives
| routine main
@ -2288,13 +2288,15 @@ a, z, and n are trashed, and must be declared as such.
a, z, and n are trashed, and must not be declared as outputs.
(Note, both n and a are unmeaningful outputs in this test.)
| byte lives
| routine main
| outputs lives, a, z, n
| {
| copy 0, lives
| }
? UnmeaningfulOutputError: n
? UnmeaningfulOutputError
Unless of course you subsequently initialize them.

View File

@ -7,7 +7,7 @@ SixtyPical to 6502 machine code.
[Falderal]: http://catseye.tc/node/Falderal
-> Functionality "Compile SixtyPical program" is implemented by
-> shell command "bin/sixtypical --output-format=c64-basic-prg --traceback %(test-body-file) >/tmp/foo && tests/appliances/bin/dcc6502-adapter </tmp/foo"
-> shell command "bin/sixtypical --output-format=c64-basic-prg --traceback %(test-body-file) --output /tmp/foo && tests/appliances/bin/dcc6502-adapter </tmp/foo"
-> Tests for functionality "Compile SixtyPical program"

View File

@ -48,7 +48,7 @@ Treat R as a mutable set and start with an empty list of lists L. Then,
- Remove all elements occurring in C, from R.
- Repeat until R is empty.
When times comes to generate code, generate it in the order given by L.
When time comes to generate code, generate it in the order given by L.
In addition, each sublist in L represents a number of routines to
generate; all except the final routine in such a sublist need not have
any jump instruction generated for its final `goto`.
@ -65,7 +65,7 @@ to pass these tests to be considered an implementation of SixtyPical.
-> shell command "bin/sixtypical --optimize-fallthru --dump-fallthru-info --analyze-only --traceback %(test-body-file)"
-> Functionality "Compile SixtyPical program with fallthru optimization" is implemented by
-> shell command "bin/sixtypical --output-format=c64-basic-prg --optimize-fallthru --traceback %(test-body-file) >/tmp/foo && tests/appliances/bin/dcc6502-adapter </tmp/foo"
-> shell command "bin/sixtypical --output-format=c64-basic-prg --optimize-fallthru --traceback %(test-body-file) --output /tmp/foo && tests/appliances/bin/dcc6502-adapter </tmp/foo"
-> Tests for functionality "Dump fallthru info for SixtyPical program"