1 votes
Line-buffered putc/getc implementation
submitted 400 days ago by lifthrasiir to Utilities
(0 comments | 585 views)
Also features newline and scroll routines. Readily usable with the current ABI and current emulators. See the comments for usage and customization.
Source Code
Download .dasm16 file |
:loop
JSR getc
; getc returns to A which is directly sent to...
JSR putc
SET PC, loop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; base screen routines
:screenoff
; keeps an offset to the current cursor.
; ranges from 0 to 0x17f; 0x180 causes the screen
; to scroll.
DAT 0
; moves the cursor to the first letter of next
; line. automatically scrolls the screen.
:newline
SET C, [screenoff]
AND C, 0xffe0
ADD C, 0x20
SET [screenoff], C
IFG 0x180, C
SET PC, POP
; *falls through*
; scrolls the screen up by one line.
:scroll
SET C, 0
:scroll_copy
SET [C+0x8000], [C+0x8020]
ADD C, 1
IFG 0x160, C
SET PC, scroll_copy
:scroll_fill
SET [C+0x8000], 0
ADD C, 1
IFG 0x180, C
SET PC, scroll_fill
SUB [screenoff], 0x20
SET PC, POP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; putc (needs base screen routines)
; writes one character to the current position.
; supports a newline character (when A=10).
; automatically scrolls the screen whenever needed.
:putc
IFE A, 10
SET PC, newline
; we need a room for a visible cursor in getc,
; so it is not 0x17f.
IFG [screenoff], 0x17e
JSR scroll
SET C, [screenoff]
BOR A, 0xf000
SET [C+0x8000], A
ADD C, 1
SET [screenoff], C
SET PC, POP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; getc (needs base screen routines)
; reads one character from the line buffer.
; line buffer is automatically filled as needed;
; the visible cursor will appear[*], the screen
; will scroll as needed, and the user can input
; any amount of text until the line buffer fills.
; backspace is supported as well.
;
; [*] currently implemented as XORing the cursor
; position with 0xff00. may behave weirdly if
; you are using a colored text; search for the
; word "cursor" if you want to fix it.
;
; the size of the line buffer is currently 128
; (including a newline character) but easily
; configurable. see lines commented with "BUFSIZE".
; be careful however: BUFSIZE should not exceed 352
; (=11*32) anyway!
:getc
; just consume the line buffer if any
IFG [inputsize], [inputoff]
SET PC, getc_buffered
; register uses:
; A = the current buffer size.
; eventually dumped to [inputsize]
; B = the current character
; C = the current keyboard buffer position,
; or the pointer to the current cursor
;
; C is clobbered by scroll and newline, but
; the code does not use C permanently anyway.
SET A, 0
SET C, [screenoff]
; draws the cursor
XOR [C+0x8000], 0xff00
:getc_wait
SET C, [keyoff]
:getc_busywait
IFE [C], 0
SET PC, getc_busywait
ADD [keyoff], 1
AND [keyoff], 0x900f
SET B, [C]
SET [C], 0
IFE B, 10
SET PC, getc_commit
IFE B, 8
SET PC, getc_bksp
; these things avoid problems with
; punctuations (may have a high bit set)
IFG B, 127
SET PC, getc_wait
IFG 32, B
SET PC, getc_wait
IFG A, 126 ; replace 126 with BUFSIZE-2
SET PC, getc_wait
SET [A+inputbuf], B
SET C, [screenoff]
BOR B, 0xf000
; will erase the cursor as well
SET [C+0x8000], B
; see putc comment
IFG C, 0x17e
JSR scroll
ADD [screenoff], 1
SET C, [screenoff]
; draws the cursor
XOR [C+0x8000], 0xff00
ADD A, 1
SET PC, getc_wait
:getc_bksp
IFE A, 0
SET PC, getc_wait
SUB A, 1
SET C, [screenoff]
; erases the cursor (cannot set to 0 since
; we keep the original screen contents as
; long as the line buffer does not overwrite
; it...)
XOR [C+0x8000], 0xff00
; the previous position is replaced with
; an empty cursor
SET [C+0x7fff], 0x0f20
SUB [screenoff], 1
SET PC, getc_wait
:getc_commit
SET C, [screenoff]
; erases the cursor
XOR [C+0x8000], 0xff00
JSR newline
SET [A+inputbuf], 10
ADD A, 1
SET [inputsize], A
SET [inputoff], 0
:getc_buffered
SET B, inputoff
SET A, [B]
SET A, [A+inputbuf]
ADD [B], 1
SET PC, POP
:keyoff
DAT 0x9000
:inputoff
DAT 0
:inputsize
DAT 0
:inputbuf
; should have BUFSIZE zeroes here
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

Comments
Sign in to comment and vote
No comments yet