1
0
mirror of https://github.com/hsoft/collapseos.git synced 2024-11-17 06:58:05 +11:00

tools/upload.py: support files larger than 0xff bytes

This commit is contained in:
Virgil Dupras 2019-06-14 15:03:38 -04:00
parent d40940a1ff
commit df5549ae76
2 changed files with 58 additions and 36 deletions

View File

@ -2,7 +2,7 @@
Collapse OS likely runs from ROM code. If you need to fiddle with your machine Collapse OS likely runs from ROM code. If you need to fiddle with your machine
more deeply, you will want to send arbitrary code to it and run it. You can do more deeply, you will want to send arbitrary code to it and run it. You can do
so with the shell's `load` and `call` commands. so with the shell's `poke` and `call` commands.
For example, let's say that you want to run this simple code that you have For example, let's say that you want to run this simple code that you have
sitting on your "modern" machine and want to execute on your running Collapse OS sitting on your "modern" machine and want to execute on your running Collapse OS
@ -22,7 +22,7 @@ Now, we'll send that code to address `0xa000`:
> mptr a000 > mptr a000
A000 A000
> load 8 (resulting binary is 8 bytes long) > poke 8 (resulting binary is 8 bytes long)
Now, at this point, it's a bit delicate. To pipe your binary to your serial Now, at this point, it's a bit delicate. To pipe your binary to your serial
connection, you have to close `screen` with CTRL+A then `:quit` to free your connection, you have to close `screen` with CTRL+A then `:quit` to free your
@ -31,7 +31,7 @@ tty device. Then, you can run:
cat tosend.bin > /dev/ttyUSB0 (or whatever is your device) cat tosend.bin > /dev/ttyUSB0 (or whatever is your device)
You can then re-open your connection with screen. You'll have a blank screen, You can then re-open your connection with screen. You'll have a blank screen,
but if the number of characters sent corresponds to what you gave `load`, then but if the number of characters sent corresponds to what you gave `poke`, then
Collapse OS will be waiting for a new command. Go ahead, verify that the Collapse OS will be waiting for a new command. Go ahead, verify that the
transfer was successful with: transfer was successful with:
@ -62,16 +62,19 @@ Success!
The serial connection is not always 100% reliable and a bad byte can slip in The serial connection is not always 100% reliable and a bad byte can slip in
when you push your code and that's not fun when you try to debug your code (is when you push your code and that's not fun when you try to debug your code (is
this bad behavior caused by my logic or by a bad serial upload?). this bad behavior caused by my logic or by a bad serial upload?). Moreover,
sending contents bigger than `0xff` bytes can be a hassle.
To this end, there is a `upload.py` file in `tools/` that takes care of loading To this end, there is a `upload.py` file in `tools/` that takes care of loading
the file and verify the contents. So, instead of doing `load 8` followed by the file and verify the contents. So, instead of doing `mptr a000` followed by
your `cat` above, you would have done: `poke 8` followed by your `cat` above, you would have done:
./upload.py /dev/ttyUSB0 tosend.bin ./upload.py /dev/ttyUSB0 a000 tosend.bin
This emits `load` and `peek` commands and fail appropriately if the `peek` This emits `mptr`, `poke` and `peek` commands and fail appropriately if the
doesn't match sent contents. Very handy. `peek` doesn't match sent contents. If the file is larger than `0xff` bytes,
repeat the process until the whole file was sent (file must fit in memory space
though, of course). Very handy.
## Labels in RAM code ## Labels in RAM code

View File

@ -24,41 +24,60 @@ def sendcmd(fd, cmd):
def main(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('device') parser.add_argument('device')
parser.add_argument('memptr')
parser.add_argument('filename') parser.add_argument('filename')
args = parser.parse_args() args = parser.parse_args()
try:
memptr = int('0x' + args.memptr, 0)
except ValueError:
print("memptr are has to be hexadecimal without prefix.")
return 1
if memptr >= 0x10000:
print("memptr out of range.")
return 1
maxsize = 0x10000 - memptr
st = os.stat(args.filename) st = os.stat(args.filename)
if st.st_size > 0xff: if st.st_size > maxsize:
print("File too big. 0xff bytes max") print("File too big. 0x{:04x} bytes max".format(maxsize))
return 1 return 1
fd = os.open(args.device, os.O_RDWR) fd = os.open(args.device, os.O_RDWR)
sendcmd(fd, 'poke {:x}'.format(st.st_size).encode())
print("Poking...")
with open(args.filename, 'rb') as fp: with open(args.filename, 'rb') as fp:
fcontents = fp.read() while True:
for c in fcontents: fcontents = fp.read(0xff)
os.write(fd, bytes([c])) if not fcontents:
# Let's give the machine a bit of time to breathe. We ain't in a break
# hurry now, are we? print("Seeking...")
time.sleep(0.0001) sendcmd(fd, 'mptr {:04x}'.format(memptr).encode())
print("Poked") os.read(fd, 9)
os.read(fd, 5) sendcmd(fd, 'poke {:x}'.format(len(fcontents)).encode())
print("Peeking back...") print("Poking...")
sendcmd(fd, 'peek {:x}'.format(st.st_size).encode()) for c in fcontents:
peek = b'' os.write(fd, bytes([c]))
while len(peek) < st.st_size * 2: # Let's give the machine a bit of time to breathe. We ain't in a
peek += os.read(fd, 1) # hurry now, are we?
time.sleep(0.0001) time.sleep(0.0001)
print("Poked")
os.read(fd, 5)
print("Peeking back...")
sendcmd(fd, 'peek {:x}'.format(len(fcontents)).encode())
peek = b''
while len(peek) < len(fcontents) * 2:
peek += os.read(fd, 1)
time.sleep(0.0001)
os.read(fd, 5)
print("Got {}".format(peek.decode()))
print("Comparing...")
for i, c in enumerate(fcontents):
hexfmt = '{:02X}'.format(c).encode()
if hexfmt != peek[:2]:
print("Mismatch at byte {}! {} != {}".format(i, peek[:2], hexfmt))
return 1
peek = peek[2:]
print("All good!")
memptr += len(fcontents)
print("Done!")
os.close(fd) os.close(fd)
print("Got {}".format(peek.decode()))
print("Comparing...")
for i, c in enumerate(fcontents):
hexfmt = '{:02X}'.format(c).encode()
if hexfmt != peek[:2]:
print("Mismatch at byte {}! {} != {}".format(i, peek[:2], hexfmt))
return 1
peek = peek[2:]
print("All good!")
return 0 return 0
if __name__ == '__main__': if __name__ == '__main__':