mirror of
https://github.com/nineties/planckforth
synced 2024-12-26 21:58:42 +01:00
replace bytearray instead of array (python version)
This commit is contained in:
parent
03c10d094d
commit
384089eb28
2 changed files with 25 additions and 34 deletions
|
@ -2513,8 +2513,8 @@ need-defined (free)
|
||||||
|
|
||||||
\ allocate heap memory
|
\ allocate heap memory
|
||||||
: %allocate ( align size -- addr e )
|
: %allocate ( align size -- addr e )
|
||||||
over + allocate ?dup unless
|
over 1- + allocate ?dup unless
|
||||||
swap 1- invert and success
|
swap 1- tuck + swap invert and success
|
||||||
then
|
then
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
|
@ -15,53 +15,45 @@ COPYRIGHT = "Copyright (c) 2021 Koichi Nakamura <koichi@idein.jp>"
|
||||||
|
|
||||||
VERSION = "{}:{}".format(RUNTIME_NAME, COPYRIGHT)
|
VERSION = "{}:{}".format(RUNTIME_NAME, COPYRIGHT)
|
||||||
|
|
||||||
MEMORY_SIZE = 0x10000
|
MEMORY_SIZE = 0x40000
|
||||||
|
|
||||||
memory = array.array('I', [0]*MEMORY_SIZE)
|
memory = bytearray(MEMORY_SIZE)
|
||||||
CELL = memory.itemsize
|
CELL = 4
|
||||||
CELLm1 = CELL - 1
|
|
||||||
CELL_SHIFT = CELL.bit_length() - 1
|
|
||||||
|
|
||||||
STACK_SIZE = 0x100
|
STACK_SIZE = 0x400
|
||||||
RSTACK_SIZE = 0x100
|
RSTACK_SIZE = 0x400
|
||||||
|
|
||||||
HERE_CELL = 0
|
HERE_CELL = 0
|
||||||
LATEST_CELL = CELL
|
LATEST_CELL = CELL
|
||||||
|
|
||||||
sp = MEMORY_SIZE * CELL
|
sp = MEMORY_SIZE
|
||||||
rp = (MEMORY_SIZE - STACK_SIZE) * CELL
|
rp = MEMORY_SIZE - STACK_SIZE
|
||||||
ip = 0
|
ip = 0
|
||||||
np = 0
|
np = 0
|
||||||
|
|
||||||
ALIGN_MASK = ~(CELL - 1)
|
ALIGN_MASK = ~(CELL - 1)
|
||||||
def aligned(n):
|
def aligned(n):
|
||||||
return (n + CELLm1) & ALIGN_MASK
|
return (n + CELL - 1) & ALIGN_MASK
|
||||||
|
|
||||||
def align():
|
def align():
|
||||||
write(HERE_CELL, aligned(read(HERE_CELL)))
|
write(HERE_CELL, aligned(read(HERE_CELL)))
|
||||||
|
|
||||||
def read(addr):
|
def read(addr, signed=False):
|
||||||
return memory[addr >> CELL_SHIFT]
|
return int.from_bytes(memory[addr:addr+CELL], 'little', signed=signed)
|
||||||
|
|
||||||
def write(addr, v):
|
def write(addr, v):
|
||||||
memory[addr >> CELL_SHIFT] = ctypes.c_uint(v).value
|
memory[addr:addr+CELL] = ctypes.c_uint32(v).value.to_bytes(CELL, 'little')
|
||||||
|
|
||||||
def comma(v):
|
def comma(v, signed=False):
|
||||||
here = read(HERE_CELL)
|
here = read(HERE_CELL)
|
||||||
write(here, v)
|
write(here, v)
|
||||||
write(HERE_CELL, here + CELL)
|
write(HERE_CELL, here + CELL)
|
||||||
|
|
||||||
def read_byte(addr):
|
def read_byte(addr):
|
||||||
i = addr >> CELL_SHIFT
|
return memory[addr]
|
||||||
m = (addr % CELL)*8
|
|
||||||
v = memory[i]
|
|
||||||
return ctypes.c_uint8((v >> m) & 0xff).value # sign extension
|
|
||||||
|
|
||||||
def write_byte(addr, c):
|
def write_byte(addr, c):
|
||||||
i = addr >> CELL_SHIFT
|
memory[addr] = c
|
||||||
m = (addr % CELL) * 8
|
|
||||||
v = memory[i]
|
|
||||||
memory[i] = (v & ~(0xff << m)) | (c&0xff) << m
|
|
||||||
|
|
||||||
def comma_byte(v):
|
def comma_byte(v):
|
||||||
here = read(HERE_CELL)
|
here = read(HERE_CELL)
|
||||||
|
@ -111,7 +103,7 @@ def push(v):
|
||||||
|
|
||||||
def pop():
|
def pop():
|
||||||
global sp
|
global sp
|
||||||
v = ctypes.c_int(read(sp)).value
|
v = read(sp, signed=True)
|
||||||
sp += CELL
|
sp += CELL
|
||||||
return v
|
return v
|
||||||
|
|
||||||
|
@ -122,7 +114,7 @@ def rpush(v):
|
||||||
|
|
||||||
def rpop():
|
def rpop():
|
||||||
global rp
|
global rp
|
||||||
v = ctypes.c_int(read(rp)).value
|
v = read(rp, signed=True)
|
||||||
rp += CELL
|
rp += CELL
|
||||||
return v
|
return v
|
||||||
|
|
||||||
|
@ -170,7 +162,7 @@ write(LATEST_CELL, 0)
|
||||||
# Store command line arguments
|
# Store command line arguments
|
||||||
argv_addrs = []
|
argv_addrs = []
|
||||||
for arg in sys.argv:
|
for arg in sys.argv:
|
||||||
argv_addrs.append(ctypes.c_int(read(HERE_CELL)).value)
|
argv_addrs.append(read(HERE_CELL))
|
||||||
comma_string(arg)
|
comma_string(arg)
|
||||||
align()
|
align()
|
||||||
ARGV_ADDR = read(HERE_CELL)
|
ARGV_ADDR = read(HERE_CELL)
|
||||||
|
@ -198,11 +190,11 @@ def key():
|
||||||
exit(0)
|
exit(0)
|
||||||
add_simple_operator('k', key)
|
add_simple_operator('k', key)
|
||||||
add_simple_operator('t', lambda: sys.stdout.write(chr(pop())))
|
add_simple_operator('t', lambda: sys.stdout.write(chr(pop())))
|
||||||
add_operator('j', lambda ip,np: next(np + ctypes.c_int(read(np)).value))
|
add_operator('j', lambda ip,np: next(np + read(np, signed=True)))
|
||||||
add_operator('J', lambda ip,np: next(np + (CELL if pop() else ctypes.c_int(read(np)).value)))
|
add_operator('J', lambda ip,np: next(np + (CELL if pop() else read(np, signed=True))))
|
||||||
add_simple_operator('f', lambda: push(find(chr(pop()))))
|
add_simple_operator('f', lambda: push(find(chr(pop()))))
|
||||||
add_operator('x', lambda ip,np: (pop(), np))
|
add_operator('x', lambda ip,np: (pop(), np))
|
||||||
add_simple_operator('@', lambda: push(ctypes.c_int(read(pop())).value))
|
add_simple_operator('@', lambda: push(read(pop(), signed=True)))
|
||||||
|
|
||||||
# NB: Python evaluates expressions from left to right
|
# NB: Python evaluates expressions from left to right
|
||||||
# https://docs.python.org/3/reference/expressions.html#evaluation-order
|
# https://docs.python.org/3/reference/expressions.html#evaluation-order
|
||||||
|
@ -222,7 +214,7 @@ add_simple_operator('R', set_rp)
|
||||||
add_simple_operator('i', lambda: push(DOCOL_ID))
|
add_simple_operator('i', lambda: push(DOCOL_ID))
|
||||||
add_operator('e', lambda ip,np: next(rpop()))
|
add_operator('e', lambda ip,np: next(rpop()))
|
||||||
def lit(ip, np):
|
def lit(ip, np):
|
||||||
push(ctypes.c_int(read(np)).value)
|
push(read(np, signed=True))
|
||||||
return next(np + CELL)
|
return next(np + CELL)
|
||||||
add_operator('L', lit)
|
add_operator('L', lit)
|
||||||
def litstring(ip, np):
|
def litstring(ip, np):
|
||||||
|
@ -281,9 +273,8 @@ add_simple_operator('(write)', writefile)
|
||||||
add_simple_operator('(read)', readfile)
|
add_simple_operator('(read)', readfile)
|
||||||
def allocate():
|
def allocate():
|
||||||
size = pop()
|
size = pop()
|
||||||
n = (size + CELL - 1) >> CELL_SHIFT
|
addr = len(memory)
|
||||||
addr = len(memory) * CELL
|
memory.extend([0]*size)
|
||||||
memory.extend([0]*n)
|
|
||||||
push(addr)
|
push(addr)
|
||||||
def free():
|
def free():
|
||||||
pop() # Bootstrap version do nothing
|
pop() # Bootstrap version do nothing
|
||||||
|
|
Loading…
Reference in a new issue