Simple program that calculates the first 10 Fibonacci numbers

This commit is contained in:
Dreaded_X 2020-01-09 00:02:12 +01:00
commit 2bdc9a5581
5 changed files with 91 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.build/

3
.vimlocal Normal file
View File

@ -0,0 +1,3 @@
set makeprg=./tools/build.sh
map <silent> <F10> :Start ./tools/upload_stm32.sh<cr>
map <silent> <F20> :Start python.exe rootfs$(pwd)/tools/upload_z80.py rootfs$(pwd)/build/fib.z80<cr>

2
build.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
mkdir -p .build && zasm -i src/fib.z80 -o .build/fib.bin

38
src/fib.z80 Normal file
View File

@ -0,0 +1,38 @@
.org $0000
main:
init:
ld hl, out+7
ld a, (amount)
ld d, a
dec d
ld a, (y)
ld b, a
ld a, (x)
loop:
add a, b
ld (hl), a
inc hl
ld c, a
ld a, b
ld b, c
dec d
jp nz, loop
done:
halt
amount:
.db 10
x:
.db 0
y:
.db 1
out:

47
upload.py Normal file
View File

@ -0,0 +1,47 @@
import serial
import os
import sys
def progressbar(it, prefix="", size=60, file=sys.stdout):
count = len(it)
def show(j):
x = int(size*j/count)
file.write("%s[%s%s] %i/%i\r" % (prefix, "#"*x, "."*(size-x), j, count))
file.flush()
show(0)
for i, item in enumerate(it):
yield item
show(i+1)
file.write("\n")
file.flush()
def main():
if len(sys.argv) == 3:
ser = serial.Serial(sys.argv[1], timeout=1, write_timeout=1)
if ser.is_open:
# Clear out any existing input
ser.write(b'\n')
ser.readline()
# Send the upload command
ser.write(b'#u\n')
print(ser.readline())
path = sys.argv[2]
size = os.path.getsize(path)
ser.write([size])
i = 0
with open(path, "rb") as f:
for i in progressbar(range(size), "Upload: ", 40):
ser.write(f.read(1))
print(ser.readline())
ser.close()
else:
print("Failed to open serial port")
else:
print("Please provide file to upload")
if "__main__":
main()