Refactor parallel comms a bit

This commit is contained in:
2025-10-01 23:24:59 +02:00
parent 3df5b28e4e
commit 2ba1546dec
8 changed files with 116 additions and 87 deletions

View File

@@ -31,11 +31,13 @@ fat12boot.elf: LDFLAGS += -T bootsect.ld
fat12boot.bin: fat12boot.elf fat12boot.bin: fat12boot.elf
stdlib.o: CFLAGS := $(filter-out -flto, $(CFLAGS))
polmon.elf: LDFLAGS += -T flat1000.ld polmon.elf: LDFLAGS += -T flat1000.ld
polmon.elf: polmon.o stdlib.o polmon.elf: polmon.o stdlib.o
polio.elf: LDFLAGS += -T flat0600.ld polio.elf: LDFLAGS += -T flat0600.ld
polio.elf: polio-main.o polio.s fat12.o paracomm.o polio.elf: polio-main.o polio.s fat12.o paracomm.o stdlib.o
wozmon.o: polmon.cc wozmon.o: polmon.cc
wozmon.o: CPPFLAGS = -DWOZMON=1 wozmon.o: CPPFLAGS = -DWOZMON=1

28
ftp.asm
View File

@@ -1,6 +1,7 @@
CPU 8086 CPU 8086
chunksize equ 0x20 chunksize equ 0x20
devicedelay equ 0x08
_start: _start:
push bp push bp
@@ -13,24 +14,35 @@ _start:
mov bx, [bp-8] mov bx, [bp-8]
mov [bx], byte 0x42 mov [bx], byte 0x42
l0: l0:
cmp word [bp-4], 0xf400 cmp word [bp-4], 0xf600
jb l1 jb l1
add sp, 8 l2:
mov sp, bp
pop bp pop bp
ret ret
l1: l1:
mov ah, 0x07
int 0x80
test al, al
jnz l1
push word [bp-2] push word [bp-2]
push word [bp-4] push word [bp-4]
mov ah, 0x05 mov ah, 0x05
int 0x80 int 0x80
add sp, 4 test al, al
jz l2
mov ah, 0
add [bp-4], ax
push word [bp-6] push word [bp-6]
push word [bp-8] push word [bp-8]
mov ah, 0x06 mov ah, 0x06
int 0x80 int 0x80
add sp, 4 add sp, 8
add word [bp-4], chunksize mov cx, devicedelay
mov cx, 0x200 l3:
l2: dec cx mov ah, 0x07
jnz l2 push cx
int 0x80
pop cx
loop l3 ; give the device some time to answer
jmp l0 jmp l0

View File

@@ -16,13 +16,14 @@ _start:
.section .bss .section .bss
kBufSize = 77 kBufSize = 77
kParaDelay = 500
kDividerRow = 23 kDividerRow = 23
kRecvBufSize = 128
curpos: .word 0 curpos: .word 0
inputsize: .byte 0 inputsize: .byte 0
escaped: .byte 0 escaped: .byte 0
inputbuf: .zero kBufSize inputbuf: .zero kBufSize
recvbuf: .zero kRecvBufSize
# XXX: funky call # XXX: funky call
# inputs: # inputs:
@@ -109,15 +110,6 @@ addtext:
pop di pop di
ret ret
.section .text.paracb
paracb:
push si
mov si, ax
mov cl, dl
call addtext
pop si
ret
.section .text.sendbuffer .section .text.sendbuffer
sendbuffer: sendbuffer:
push bp push bp
@@ -167,22 +159,17 @@ drawchar:
.section .text.parasend .section .text.parasend
parasend: parasend:
1: mov ah, 0x06 # send parallel
test cl, cl
jz 0f
mov al, [si]
push cx push cx
call paracomm_send push si
pop cx int 0x80
test al, al mov cx, 1
jnz 2f push cx
inc si mov cx, offset .l1 + 1
dec cl push cx
jmp 1b mov ah, 0x06 # send parallel
0: int 0x80
mov al, '\n' add sp, 8
call paracomm_send
2:
ret ret
prompt: prompt:
@@ -212,8 +199,6 @@ main:
movb escaped, 0 movb escaped, 0
mov ax, 0x0002 mov ax, 0x0002
int 0x10 int 0x10
mov ax, offset paracb
call paracomm_init
mov si, offset .l2 mov si, offset .l2
mov cl, l2len mov cl, l2len
call addtext call addtext
@@ -259,21 +244,18 @@ main:
4: 4:
call sendbuffer # send input buffer call sendbuffer # send input buffer
1: 1:
call paracomm_nextbyte mov ah, 0x07 # do parallel comms
mov dx, 0x3bc int 0x80
out dx, al mov ax, kRecvBufSize
add dl, 2 push ax
mov al, 1 mov ax, offset recvbuf
out dx, al push ax
mov cx, kParaDelay mov ah, 0x05 # recv
2: loop 2b int 0x80
mov al, 0 test al, al
out dx, al mov cl, al
mov cx, kParaDelay pop si
3: loop 3b pop bx
dec dl jz 0b
in al, dx call addtext
mov cl, 4
shr al, cl
call paracomm_feed
jmp 0b jmp 0b

View File

@@ -1,6 +1,7 @@
#include "paracomm.h" #include "paracomm.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#define kMosiIdleByte 0x00 #define kMosiIdleByte 0x00
#define kMosiStartByte 0x42 #define kMosiStartByte 0x42
@@ -21,7 +22,6 @@ static uint8_t miso_recvbuf[256];
static uint8_t miso_size; static uint8_t miso_size;
static uint8_t miso_received_nibbles; static uint8_t miso_received_nibbles;
static uint8_t miso_state; static uint8_t miso_state;
static void (*miso_cb)(const uint8_t*, uint8_t);
static void swapbuffers() { static void swapbuffers() {
mosi_sendbuf = mosi_workbuf[mosi_workbuf_idx]; mosi_sendbuf = mosi_workbuf[mosi_workbuf_idx];
@@ -92,23 +92,19 @@ void paracomm_feed(uint8_t n) {
miso_received_nibbles += 1; miso_received_nibbles += 1;
if (miso_received_nibbles == 2 * miso_size) { if (miso_received_nibbles == 2 * miso_size) {
if (miso_cb != 0) {
miso_cb(miso_recvbuf, miso_size);
}
miso_state = 0; miso_state = 0;
} }
} break; } break;
} }
} }
void paracomm_init(miso_cb_t cb) { void paracomm_init() {
mosi_size = 0; mosi_size = 0;
mosi_workbuf_idx = 0; mosi_workbuf_idx = 0;
mosi_workbuf_size = 0; mosi_workbuf_size = 0;
mosi_state = 0; mosi_state = 0;
miso_state = 0; miso_state = 0;
miso_cb = cb;
} }
int paracomm_send(uint8_t b) { int paracomm_send(uint8_t b) {
@@ -122,3 +118,21 @@ int paracomm_send(uint8_t b) {
return 0; return 0;
} }
uint8_t paracomm_recv(uint8_t* buf, uint8_t size) {
if (miso_state != 0) {
return 0;
}
if (size > miso_size) {
size = miso_size;
}
memcpy(buf, miso_recvbuf, size);
memcpy(miso_recvbuf, miso_recvbuf+size, miso_size - size);
miso_size -= size;
return size;
}
uint8_t paracomm_busy() {
return mosi_state != 0 || miso_state != 0;
}

View File

@@ -2,18 +2,24 @@
#include <stdint.h> #include <stdint.h>
typedef void (*miso_cb_t)(const uint8_t* buff, uint8_t size);
/** Must call first **/ /** Must call first **/
void paracomm_init(miso_cb_t miso_cb); void paracomm_init();
/** Sends a single byte. /** Sends a single byte.
* Returns: 0 if no error, non-0 otherwise. * Returns: 0 if no error, non-0 otherwise.
*/ */
int paracomm_send(uint8_t b); int paracomm_send(uint8_t b);
/** Receive some bytes.
* Returns: number of bytes copied
*/
uint8_t paracomm_recv(uint8_t* buf, uint8_t size);
/** Call after reading a nibble from the port */ /** Call after reading a nibble from the port */
void paracomm_feed(uint8_t n); void paracomm_feed(uint8_t n);
/** Yields the next byte to send out the port */ /** Yields the next byte to send out the port */
uint8_t paracomm_nextbyte(); uint8_t paracomm_nextbyte();
/** Returns non-zero if busy */
uint8_t paracomm_busy();

View File

@@ -1,4 +1,6 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "fat12.h" #include "fat12.h"
#include "paracomm.h" #include "paracomm.h"
@@ -11,21 +13,18 @@
#define kParallelStatusPort 0x3bd #define kParallelStatusPort 0x3bd
#define kParallelControlPort 0x3be #define kParallelControlPort 0x3be
#define kParaDelay 400 #define kParaDelay 400
#define kRecvBufferSize 256
void dosdbt(); void dosdbt();
void int80h(); void int80h();
uint8_t* parabuf; uint8_t parabuf[kRecvBufferSize];
uint8_t parasize; uint8_t parasize;
static void paracb(const uint8_t* buf, uint8_t size) { static void paracb(const uint8_t* buf, uint8_t size) {
if (size > parasize) { // we'll mercilessly erase old data with new data
return paracb(buf, parasize); memcpy(parabuf, buf, size);
} parasize = size;
for (int i = 0; i < size; i++) {
parabuf[i] = buf[i];
}
parasize = 0;
} }
static void die() { static void die() {
@@ -52,7 +51,7 @@ static void __delay__(int n) {
} }
} }
static void paraxfer() { uint8_t xferpara() {
uint8_t mosib = paracomm_nextbyte(); uint8_t mosib = paracomm_nextbyte();
__outb__(kParallelDataPort, mosib); __outb__(kParallelDataPort, mosib);
__outb__(kParallelControlPort, 1); __outb__(kParallelControlPort, 1);
@@ -61,30 +60,23 @@ static void paraxfer() {
__delay__(kParaDelay); __delay__(kParaDelay);
uint8_t mison = __inb__(kParallelStatusPort) >> 4; uint8_t mison = __inb__(kParallelStatusPort) >> 4;
paracomm_feed(mison); paracomm_feed(mison);
return paracomm_busy();
} }
void pararecv(uint8_t* addr, uint8_t size) { uint8_t parasend(uint8_t* addr, uint8_t size) {
parabuf = addr; uint8_t i;
parasize = size; for (i = 0; i < size; i++) {
for (int i = 0; i < 1 + 2 + size * 2; i++) { if (paracomm_send(addr[i])) {
if (!parasize) {
break; break;
} }
paraxfer();
}
}
void parasend(uint8_t* addr, uint8_t size) {
for (int i = 0; i < size; i++) {
paracomm_send(addr[i]);
}
for (int i = 0; i < size + 2; i++) {
paraxfer();
} }
return i;
} }
int main() { int main() {
dosdbt(); dosdbt();
parasize = 0;
paracomm_init(paracb); paracomm_init(paracb);
fat12_init(kFatAddr, kRootDirAddr); fat12_init(kFatAddr, kRootDirAddr);

View File

@@ -143,7 +143,7 @@ recvpara:
mov ax, [bp+0] mov ax, [bp+0]
add ax, si add ax, si
mov dx, [bp+2] mov dx, [bp+2]
call pararecv call paracomm_recv
ret ret
.section .text.int80h .section .text.int80h
@@ -186,5 +186,6 @@ int80h_table:
.word offset readfile # 0x04 .word offset readfile # 0x04
.word offset recvpara # 0x05 .word offset recvpara # 0x05
.word offset sendpara # 0x06 .word offset sendpara # 0x06
.word offset xferpara # 0x07
int80h_entries = . - int80h_table int80h_entries = . - int80h_table

View File

@@ -1,3 +1,6 @@
#include <stdint.h>
#include <stdlib.h>
int getchar() { int getchar() {
register char c asm("al"); register char c asm("al");
asm volatile("movb $0x00, %%ah\n\t" asm volatile("movb $0x00, %%ah\n\t"
@@ -23,3 +26,20 @@ int puts(const char* s) {
} }
return 0; return 0;
} }
void* memcpy(void* dest, const void* src, size_t n) {
uint8_t* d = dest;
const uint8_t* s = src;
for (int i = 0; i < n; i++) {
d[i] = s[i];
}
return dest;
}
void* memset(void* ptr, int val, size_t len) {
uint8_t* p = ptr;
while (len--) {
*p++ = val;
}
return ptr;
}