Skip to content

Commit 66de27e

Browse files
Save 12 bytes by inlining functions--thanks Ray!
1 parent 3e6a093 commit 66de27e

3 files changed

Lines changed: 33 additions & 55 deletions

File tree

src/functions.s

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -183,32 +183,6 @@ fun_dpeek:
183183
pla
184184
rts
185185

186-
.bss
187-
188-
rnd_value: .res .sizeof(Float::t)
189-
190-
.code
191-
192-
fun_rnd:
193-
lda FP0e ; 0 -> return previous number, >0 -> return next number, <0 -> reseed
194-
beq @output
195-
lda FP0s
196-
bpl @generate
197-
jsr rnd_reseed
198-
@generate:
199-
jsr rnd_generate
200-
@output:
201-
ldx #4 ; Make random number from rnd_value
202-
@next_copy_to_fp0:
203-
lda rnd_value-1,x
204-
sta FP0t-1,x
205-
dex
206-
bne @next_copy_to_fp0
207-
lda #BIAS-1 ; This effectively puts the binary point to the left of the mantissa
208-
sta FP0e
209-
stx FP0s ; The purpose of all the -1s was to make X 0 here
210-
jmp normalize
211-
212186
fun_sgn:
213187
lda FP0e ; If exponent is 0 then value is 0; return 0
214188
beq @done

src/random.s

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,27 @@
66
; Separated out from the rest of functions so we can replace it on a machine that includes a hardware random
77
; number generator.
88

9-
rnd_mask: .byte $B7, $1D, $C1, $04
9+
.bss
10+
11+
rnd_value: .res .sizeof(Float::t)
1012

11-
; Generates a new random value in rnd_value.
12-
; Uses a 32-bit linear feedback shift register (LFSR) with taps defined by rnd_mask.
13-
; To generate one random bit, we shift rnd_value left one bit and then, if the bit we shifted into the carry is 1, we
14-
; XOR the seed with the rnd_mask. The random bit is the bit we shifted off the left, but we don't use it, because in
15-
; fact the value left in rnd_value contains the *last* value returned from the RND function. We we generate 32 new
16-
; bits and then just return them directly from rnd_value. The value of rnd_mask is the CRC32 polynomial and will
17-
; generate random numbers with a cycle of 2^31-1.
13+
.code
14+
15+
rnd_mask: .byte $B7, $1D, $C1, $04
1816

19-
rnd_generate:
17+
; Generates a new random value in rnd_value and outputs it in FP0.
18+
fun_rnd:
19+
lda FP0e ; 0 -> return previous number, >0 -> return next number, <0 -> reseed
20+
beq @output
21+
lda FP0s
22+
bpl @generate
23+
ldx #4 ; Copy given number into rnd_value
24+
@next_copy_to_value:
25+
lda FP0t-1,x
26+
sta rnd_value-1,x
27+
dex
28+
bne @next_copy_to_value
29+
@generate:
2030
ldy #32 ; Each iteration generates 1 pseudo-random bit
2131
@next_shift:
2232
asl rnd_value
@@ -34,13 +44,14 @@ rnd_generate:
3444
@skip_feedback:
3545
dey
3646
bne @next_shift
37-
rts
38-
39-
rnd_reseed:
40-
ldx #4 ; Copy given number into rnd_value
41-
@next_copy_to_value:
42-
lda FP0t-1,x
43-
sta rnd_value-1,x
47+
@output:
48+
ldx #4 ; Make random number from rnd_value
49+
@next_copy_to_fp0:
50+
lda rnd_value-1,x
51+
sta FP0t-1,x
4452
dex
45-
bne @next_copy_to_value
46-
rts
53+
bne @next_copy_to_fp0
54+
lda #BIAS-1 ; This effectively puts the binary point to the left of the mantissa
55+
sta FP0e
56+
stx FP0s ; The purpose of all the -1s was to make X 0 here
57+
jmp normalize

src/string.s

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,10 @@ for_all_strings:
249249
lda #STRING_EXTRA - 2 ; Move to next string; minus 2 because two calls to "plus_one" function
250250
jsr add_src_ptr_plus_one
251251
@next:
252-
jsr check_src_ptr
252+
lda src_ptr ; Compare src_ptr with himem_ptr using 16-bit subtraction
253+
cmp himem_ptr ; Check low byte
254+
lda src_ptr+1 ; Subtract high byte
255+
sbc himem_ptr+1 ; Carry clear if src_ptr < himem_ptr, else carry set
253256
bcc @continue
254257
rts
255258

@@ -463,16 +466,6 @@ set_name_ptr_data:
463466
txa ; Return type in A (setting flags)
464467
rts
465468

466-
; Checks if src_ptr is < himem_ptr.
467-
; Returns carry clear if it is, otherwise carry set.
468-
469-
check_src_ptr:
470-
lda src_ptr ; Compare src_ptr with himem_ptr using 16-bit subtraction
471-
cmp himem_ptr ; Check low byte
472-
lda src_ptr+1 ; Subtract high byte
473-
sbc himem_ptr+1 ; Returns with carry clear if src_ptr < himem_ptr, else carry set
474-
@done:
475-
rts
476469

477470
; Adds the value in A plus one to src_ptr. Always adds one more than A to make make skipping over the length byte
478471
; and string data easier.

0 commit comments

Comments
 (0)