| 1 |
;ACME 0.97 |
| 2 |
|
| 3 |
; here's an implementation of the "double dabble" algorithm, a quick |
| 4 |
; and easy way to output integers as decimal digits. |
| 5 |
|
| 6 |
; in this example, the 24-bit value of the time variable TI is shown |
| 7 |
; on the screen as eight decimal digits, updated once per frame (every |
| 8 |
; once in a while there will be a glitch because the system interrupt |
| 9 |
; handler is not synchronized with the screen, to fix this is left as |
| 10 |
; an excercise to the reader). |
| 11 |
; the raster time used is indicated via changes of the border color. |
| 12 |
|
| 13 |
DIGITS = 8 ; three input bytes -> 24 bits -> max 16777215 -> we need eight digits |
| 14 |
!addr { |
| 15 |
ti_high = $a0 ; input data |
| 16 |
ti_med = $a1 |
| 17 |
ti_low = $a2 |
| 18 |
byte = $fb ; buffers input byte during shifts |
| 19 |
out = $0400 ; where to show output digits (big-endian!) |
| 20 |
raster = $d012 ; to sync with screen |
| 21 |
border = $d020 ; to show raster time |
| 22 |
} |
| 23 |
* = $0801 |
| 24 |
!src "misc/basicstub.a" |
| 25 |
--- ; wait for lower border |
| 26 |
lda raster |
| 27 |
cmp #251 |
| 28 |
bne --- |
| 29 |
inc border ; start "stop watch" |
| 30 |
jsr dd_clear ; clear output |
| 31 |
sei ; make sure TI is stable while we read it |
| 32 |
lda ti_low |
| 33 |
pha |
| 34 |
lda ti_med |
| 35 |
pha |
| 36 |
lda ti_high |
| 37 |
cli ; re-allow irq |
| 38 |
jsr dd_process_A ; process high byte |
| 39 |
pla ; process medium byte |
| 40 |
jsr dd_process_A |
| 41 |
pla ; process low byte |
| 42 |
jsr dd_process_A |
| 43 |
jsr dd_result ; convert result from 0..9 values to '0'..'9' characters |
| 44 |
dec border ; stop "stop watch" |
| 45 |
jmp --- |
| 46 |
|
| 47 |
dd_clear ; clear result bytes |
| 48 |
ldx #DIGITS - 1 |
| 49 |
lda #0 |
| 50 |
--- sta out, x |
| 51 |
dex |
| 52 |
bpl --- |
| 53 |
rts |
| 54 |
|
| 55 |
dd_result ; convert result bytes to ascii digits |
| 56 |
ldx #DIGITS - 1 |
| 57 |
--- lda out, x |
| 58 |
ora #$30 |
| 59 |
sta out, x |
| 60 |
dex |
| 61 |
bpl --- |
| 62 |
rts |
| 63 |
|
| 64 |
dd_process_A ; process eight bits |
| 65 |
; use the zero-means-empty trick so we do not need a bit counter: |
| 66 |
sec ; create "marker bit" |
| 67 |
rol ; push into A, now C = data bit |
| 68 |
sta byte ; remember for later |
| 69 |
; here's how to process a bit: |
| 70 |
; "out" holds digits (0..9, not '0'..'9'), |
| 71 |
; but in big-endian order (for screen output). |
| 72 |
; the new bit is in C: |
| 73 |
next_bit ldx #DIGITS - 1 ; loop over output digits |
| 74 |
next_digit ldy out, x ; now Y = 0..9 (old value) |
| 75 |
lda table, y ; now A = 0/1/2/3/4/128/129/130/131/132 |
| 76 |
rol ; now A = 0..9 (new value), bit for next digit in C |
| 77 |
sta out, x ; update digit |
| 78 |
dex |
| 79 |
bpl next_digit |
| 80 |
;clc ; only needed if overflow possible |
| 81 |
rol byte ; get next data bit |
| 82 |
bne next_bit |
| 83 |
; byte is empty (C is 1, but that's the marker bit) |
| 84 |
rts |
| 85 |
table |
| 86 |
!by 0, 1, 2, 3, 4, $80, $81, $82, $83, $84 |