1
0
mirror of https://github.com/catseye/SixtyPical.git synced 2025-08-14 17:27:57 +00:00

A byte or word table can be initialized with a list of constants.

This commit is contained in:
Chris Pressey
2018-03-29 11:09:02 +01:00
parent 2f513f7291
commit eadf1eb4ae
5 changed files with 36 additions and 16 deletions

View File

@@ -6,10 +6,11 @@ History of SixtyPical
* Symbolic constants can be defined with the `const` keyword, and can * Symbolic constants can be defined with the `const` keyword, and can
be used in most places where literal values can be used. be used in most places where literal values can be used.
* Specifying multiple SixtyPical source files will produce a single
compiled result from their combination.
* Added `nop` opcode, which compiles to `NOP` (mainly for timing.) * Added `nop` opcode, which compiles to `NOP` (mainly for timing.)
* Accessing zero-page with `ld` and `st` generates zero-page opcodes. * Accessing zero-page with `ld` and `st` generates zero-page opcodes.
* A `byte` or `word` table can be initialized with a list of constants.
* Specifying multiple SixtyPical source files will produce a single
compiled result from their combination.
* Rudimentary support for Atari 2600 prelude in a 4K cartridge image, * Rudimentary support for Atari 2600 prelude in a 4K cartridge image,
and start of an example program in `eg/atari2600` directory. and start of an example program in `eg/atari2600` directory.

View File

@@ -122,8 +122,9 @@ class Compiler(object):
elif type_ == TYPE_WORD: elif type_ == TYPE_WORD:
initial_data = Word(defn.initial) initial_data = Word(defn.initial)
elif TableType.is_a_table_type(type_, TYPE_BYTE): elif TableType.is_a_table_type(type_, TYPE_BYTE):
# FIXME convert defn.initial to a serializable type ... or when parsing. initial_data = Table([Byte(i) for i in defn.initial], type_.size)
initial_data = Table(defn.initial, type_.size) elif TableType.is_a_table_type(type_, TYPE_WORD):
initial_data = Table([Word(i) for i in defn.initial], type_.size)
else: else:
raise NotImplementedError(type_) raise NotImplementedError(type_)
label.set_length(initial_data.size()) label.set_length(initial_data.size())

View File

@@ -13,6 +13,8 @@ class Emittable(object):
class Byte(Emittable): class Byte(Emittable):
def __init__(self, value): def __init__(self, value):
if isinstance(value, basestring):
value = ord(value)
if value < -127 or value > 255: if value < -127 or value > 255:
raise IndexError(value) raise IndexError(value)
if value < 0: if value < 0:
@@ -49,6 +51,7 @@ class Word(Emittable):
class Table(Emittable): class Table(Emittable):
def __init__(self, value, size): def __init__(self, value, size):
"""`value` should be an iterable of Emittables."""
# TODO: range-checking # TODO: range-checking
self.value = value self.value = value
self._size = size self._size = size
@@ -57,12 +60,10 @@ class Table(Emittable):
return self._size return self._size
def serialize(self, addr=None): def serialize(self, addr=None):
bytes = [] buf = ''.join([emittable.serialize() for emittable in self.value])
for b in self.value: while len(buf) < self.size():
bytes.append(chr(ord(b))) buf += chr(0)
while len(bytes) < self.size(): return buf
bytes.append(chr(0))
return ''.join(bytes)
def __repr__(self): def __repr__(self):
return "%s()" % (self.__class__.__name__) return "%s()" % (self.__class__.__name__)

View File

@@ -144,9 +144,9 @@ class Parser(object):
self.scanner.scan() self.scanner.scan()
else: else:
initial = [] initial = []
initial.append(self.const()) initial.append(self.const().value)
while self.scanner.consume(','): while self.scanner.consume(','):
initial.append(self.const()) initial.append(self.const().value)
else: else:
initial = self.const().value initial = self.const().value

View File

@@ -198,14 +198,31 @@ Initialized byte table, initialized with list of byte values.
= $080D LDX #$00 = $080D LDX #$00
= $080F LDA $0813,X = $080F LDA $0813,X
= $0812 RTS = $0812 RTS
= $0813 .byte $57 = $0813 .byte $FF
= $0814 PHA = $0814 BRK
= $0815 EOR ($54,X) = $0815 STA ($80,X)
= $0817 .byte $3F = $0817 .byte $7F
= $0818 BRK = $0818 BRK
= $0819 BRK = $0819 BRK
= $081A BRK = $081A BRK
Initialized word table, initialized with list of word values.
| word table[8] message : 65535, 0, 127
|
| routine main
| {
| }
= $080D RTS
= $080E .byte $FF
= $080F .byte $FF
= $0810 BRK
= $0811 BRK
= $0812 .byte $7F
= $0813 BRK
= $0814 BRK
= $0815 BRK
Some instructions. Some instructions.
| byte foo | byte foo