diff --git a/assembly/os/drivers/arduino_terminal.asm b/assembly/os/drivers/arduino_terminal.asm index 12de40f..c8727b5 100644 --- a/assembly/os/drivers/arduino_terminal.asm +++ b/assembly/os/drivers/arduino_terminal.asm @@ -61,17 +61,14 @@ Term_readline: ld bc, incoming_string ; Returns read string pointer ret -; Returns the number of bytes available on the parallel port using the -; DATA_AVAILABLE register. -; @return a the number of available bytes -Term_availb: - in a, (TERM_DATA_AVAIL_REG) - ret - ; Reads the first available byte on the serial port using the DATA register. +; Waits for the Terminal DATA_AVAILABLE register to be non-zero before reading. ; 0s are not ignored (cannot be used with keyboard) ; Affects NO condition bits! ; @return the available byte, even if 0 Term_readb: + in a, (TERM_DATA_AVAIL_REG) + cp 0 + jp z, Term_readb in a, (TERM_DATA_REG) ; reads a byte ret \ No newline at end of file diff --git a/assembly/os/monitor.asm b/assembly/os/monitor.asm index c894c9d..0037f42 100644 --- a/assembly/os/monitor.asm +++ b/assembly/os/monitor.asm @@ -274,8 +274,8 @@ monitor_adb: ; start copying incoming data to application space call monitor_copyTermToAppMem ;jp APP_SPACE ; Start executing code - ld bc, APP_SPACE - call Sys_Print + ; ld bc, APP_SPACE + ; call Sys_Print jp monitor_main_loop ; Prints "0x" and read 1 hex byte (2 hex digits, e.g. 0x8C) @@ -482,11 +482,6 @@ monitor_copyTermToAppMem: ld d, 2 ld hl, APP_SPACE ; we will write in APP_SPACE monitor_copyTermToAppMem_loop: - ; check if bytes are available - call Term_availb - cp 0 - jp z, monitor_copyTermToAppMem ; no bytes available, next loop - ; bytes are available ld a, d cp 2 ; check if we are receiving first header byte jp z, monitor_copyTermToAppMem_loop_rec_head_byte_1 @@ -494,20 +489,29 @@ monitor_copyTermToAppMem: cp 1 ; check if we are receiving second header byte jp z, monitor_copyTermToAppMem_loop_rec_head_byte_2 ; we are receiving binary stream: read byte and save to memory - call Term_readb + call Term_readb ; reads a byte from terminal ld (hl), a ; copy byte to memory inc hl ; move to next memory position - jp monitor_copyTermToAppMem_loop ; continue loop + dec bc ; decrement remaining bytes counter + ; check if we reached the number of bytes to be transferred + ld a, b + cp 0 + jp nz, monitor_copyTermToAppMem_loop ; continue loop + ld a, c + cp 0 + jp nz, monitor_copyTermToAppMem_loop ; continue loop + ; all bytes received, return + ret monitor_copyTermToAppMem_loop_rec_head_byte_1: ; we are receiving first header byte: read byte and save to b - call Term_readb + call Term_readb ; reads a byte from terminal ld b, a dec d jp monitor_copyTermToAppMem_loop ; continue loop monitor_copyTermToAppMem_loop_rec_head_byte_2: ; we are receiving second header byte: read byte and save to c - call Term_readb + call Term_readb ; reads a byte from terminal ld c, a dec d jp monitor_copyTermToAppMem_loop ; continue loop diff --git a/python/terminal_emulator.py b/python/terminal_emulator.py index b8d8932..18ed302 100755 --- a/python/terminal_emulator.py +++ b/python/terminal_emulator.py @@ -31,10 +31,13 @@ import os class TerminalEmulator: + SYNC_SLEEP = 0.001 + def __init__(self, w, ser): w.clear() w.move(0,0) while True: + # read serial port and write to curses if ser.inWaiting(): b = ser.read(1) @@ -45,6 +48,7 @@ class TerminalEmulator: key = w.getch() if key == 10 or (key > 31 and key < 256): # Is a character + time.sleep(self.SYNC_SLEEP) ser.write(bytes([key])) elif int(key) == 1: # CTRL+A, enter ADB mode # Save cursor position @@ -67,20 +71,29 @@ class TerminalEmulator: w.addstr(0, 0, '[ADB MODE] file to load:', curses.A_REVERSE) path = w.getstr() try: + header = bytearray(2) size = os.path.getsize(path) # Compute the two header bytes needed to declare the length of the stream - h1 = size & 255 # get lower 8 bits + header[1] = size & 255 # get lower 8 bits size >>= 8 # shift by 8 bits - h0 = size & 255 # get second lower 8 bits + header[0] = size & 255 # get second lower 8 bits + # Log size for debug + w.addstr("Header bytes: {}".format(header.hex())) + w.refresh() + # Send the two heading bytes (most significant first) - ser.write(h0) - ser.write(h1) + ser.write(header[0:1]) + time.sleep(self.SYNC_SLEEP) + ser.write(header[1:2]) + time.sleep(self.SYNC_SLEEP) + # Send the actual binary stream with open(path, "rb") as f: byte = f.read(1) while byte: ser.write(byte) byte = f.read(1) + time.sleep(self.SYNC_SLEEP) except IOError as e: w.move(0,0) w.clrtoeol()