Skip to content

Commit

Permalink
Recoder RND(x) pour rendre le code plus clair
Browse files Browse the repository at this point in the history
  • Loading branch information
Picatout committed Apr 15, 2023
1 parent eea77b3 commit 592e7b0
Show file tree
Hide file tree
Showing 14 changed files with 623 additions and 580 deletions.
128 changes: 67 additions & 61 deletions TinyBasic.asm
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
;
; * variables return their value in A:X
;
; * relation, expression return value on xstack
;---------------------------------------------------


Expand Down Expand Up @@ -172,7 +171,7 @@ move_exit:
;-----------------------
MAJOR=3
MINOR=1
REV=13
REV=14
software: .asciz "\n\nTiny BASIC for STM8\nCopyright, Jacques Deschenes 2019,2022,2023\nversion "
board:
Expand Down Expand Up @@ -4210,17 +4209,57 @@ arduino_to_8s207:
.endif


;------------------------------
; BASIC: RND(expr)
; return random number
; between 1 and expr inclusive
; xorshift16
; ref: http://b2d-f9r.blogspot.com/2010/08/16-bit-xorshift-rng-now-with-more.html
; input:
; none
; output:
; xstack random positive integer
;------------------------------
;---------------------------------
; seedx:seedy= x:y ^ seedx:seedy
; X:Y = seedx:seedy
;---------------------------------
xor_seed32:
ld a,xh
_xorz seedx
_straz seedx
ld a,xl
_xorz seedx+1
_straz seedx+1
ld a,yh
_xorz seedy
_straz seedy
ld a,yl
_xorz seedy+1
_straz seedy+1
_ldxz seedx
_ldyz seedy
ret

;-----------------------------------
; x:y= x:y << a
;-----------------------------------
sll_xy_32:
sllw y
rlcw x
dec a
jrne sll_xy_32
ret

;-----------------------------------
; x:y= x:y >> a
;-----------------------------------
srl_xy_32:
srlw x
rrcw y
dec a
jrne srl_xy_32
ret

;---------------------------------
; BASIC: RND(n)
; return integer [0..n-1]
; ref: https://en.wikipedia.org/wiki/Xorshift
;
; x ^= x << 13;
; x ^= x >> 17;
; x ^= x << 5;
;
;---------------------------------
N1=1
N2=N1+INT_SIZE
VSIZE=6
Expand All @@ -4238,55 +4277,22 @@ func_random:
2$:
_i24_fetch N1
_i24_store N2
; acc16=(x<<5)^x
_ldxz seedx
ld a,#13
call sll_xy_32
call xor_seed32
ld a,#17
call srl_xy_32
call xor_seed32
ld a,#5
call sll_xy_32
call xor_seed32
; return seedy_lo&0x7f:seedx modulo expr + 1
_ldaz seedy+1
and a,#0x7f
ld (N1,sp),a
_ldxz seedx
sllw x
sllw x
sllw x
sllw x
sllw x
ld a,xh
xor a,seedx
_straz acc16
ld a,xl
xor a,seedx+1
_straz acc8
; seedx=seedy
_ldxz seedy
ldw seedx,x
; seedy=seedy^(seedy>>1)
_ldxz seedy
srlw x
ld a,xh
xor a,seedy
_straz seedy
ld a,xl
xor a,seedy+1
_straz seedy+1
; acc16>>3
_ldxz acc16
srlw x
srlw x
srlw x
; x=acc16^x
ld a,xh
xor a,acc16
_straz acc16
ld a,xl
xor a,acc8
_straz acc8
; seedy=acc16^seedy
xor a,seedy+1
ld xl,a
_ldaz acc16
xor a,seedy
ld xh,a
ldw seedy,x
; return seedx_lo&0x7f:seedy modulo expr + 1
_ldaz seedx+1
and a,#127
ld (N1,SP),A
ldw (N1+1,SP),X
ldw (N1+1,sp),x
call div24
LD A,(N2,SP)
LDW X,(N2+1,SP) ; remainder
Expand Down
2 changes: 2 additions & 0 deletions arithm24.asm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
;; X bits 15..0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SEPARATE=0

.if SEPARATE
.module ARITHM24
.include "config.inc"
Expand Down
Binary file modified build/stm8s207k8/TinyBasic.bin
Binary file not shown.
168 changes: 84 additions & 84 deletions build/stm8s207k8/TinyBasic.ihx

Large diffs are not rendered by default.

Binary file modified build/stm8s208rb/TinyBasic.bin
Binary file not shown.
851 changes: 430 additions & 421 deletions build/stm8s208rb/TinyBasic.ihx

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions config.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

DEBUG=0 ; set to 1 to include debugging code

SEPARATE=0 ; set to 1 for separate assembly

WANT_IWDG=0 ; set to 1 to add words IWDGEN and IWDGREF

; boards list
Expand All @@ -15,6 +13,7 @@ NUCLEO_8S208RB=0
; only one is selected
.if NUCLEO_8S208RB
NUCLEO_8S207K8=0
SB5_SHORT=0
.else
NUCLEO_8S207K8=1
.endif
Expand Down
2 changes: 2 additions & 0 deletions debug_support.asm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
;; to enable it.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SEPARATE=0

.if SEPARATE
.module DBG_SUPPORT
.include "config.inc"
Expand Down
2 changes: 2 additions & 0 deletions flash_prog.asm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
;;; FLASH, EEPROM and OPTION memory
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SEPARATE=0

.if SEPARATE
.module FLASH_PROG
.include "config.inc"
Expand Down
2 changes: 2 additions & 0 deletions i2c.asm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
; along with stm8_tbi. If not, see <http://www.gnu.org/licenses/>.
;;

SEPARATE=0

.if SEPARATE
.module I2C
.include "config.inc"
Expand Down
15 changes: 15 additions & 0 deletions inc/gen_macros.inc
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,21 @@
.byte 0xbe,v
.endm

; load y from variable in zero page
.macro _ldyz v
.byte 0x90,0xbe,v
.endm

; store x in zero page variable
.macro _strxz v
.byte 0xbf,v
.endm

; store y in zero page variable
.macro _stryz v
.byte 0x90,0xbf,v
.endm

; increment 16 bits variable
; use 10 bytes
.macro _incwz v
Expand All @@ -97,3 +107,8 @@
_incz v ; 1 cy, 2 bytes
.endm ; 3 cy

; xor op with zero page variable
.macro _xorz v
.byte 0xb8,v
.endm
4 changes: 4 additions & 0 deletions journal.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 2023-04-14

* Réécriture de la fonction **RND()**.

### 2023-03-09

* Corrigé bogue dans la commande **TONE** qui ne tenait pas compte de la commutation entre CLK_HSE et CLK_HSI.
Expand Down
2 changes: 2 additions & 0 deletions terminal.asm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
; print_hex print hex value from A
;------------------------------

SEPARATE=0

.if SEPARATE
.module TERMINAL
.include "config.inc"
Expand Down
24 changes: 12 additions & 12 deletions xmodem.asm
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ wait_soh:
ldw y,timer
jrne 2$
ret
2$: call uart3_qgetc
2$: call uart_qgetc
jreq 1$
call uart3_getc
call uart_getc
cp a,#SOH
jreq 4$
cp a,#EOT
Expand Down Expand Up @@ -82,9 +82,9 @@ getc_to::
1$: ldw y,timer
jrne 2$
ret
2$: call uart3_qgetc
2$: call uart_qgetc
jreq 1$
call uart3_getc
call uart_getc
clrw y
ldw timer,y
ret
Expand Down Expand Up @@ -117,7 +117,7 @@ try_again:
cp a,#EOT
jrne 2$
ld a,#ACK
call uart3_putc
call uart_putc
ld a,#EOT
jra 7$
1$: ;start of header received
Expand All @@ -132,7 +132,7 @@ try_again:
jreq 4$
2$:
ld a,#NAK
call uart3_putc
call uart_putc
dec (TRIES,sp)
jrne try_again
jra 5$
Expand All @@ -154,7 +154,7 @@ try_again:
jra 6$
5$: ; all tries failed
ld a,#CAN
6$: call uart3_putc
6$: call uart_putc
7$: _drop VAR_SIZE
ret

Expand All @@ -179,25 +179,25 @@ xtrmt_block::
ld (TRIES,sp),a
tx_retries:
ld a,#SOH
call uart3_putc
call uart_putc
ld a,(SERIAL,sp)
call uart3_putc
call uart_putc
ld a,(SERIAL,sp)
cpl a
call uart3_putc
call uart_putc
clr (CHKSUM,sp)
ld a,#PACKET_SIZE
ld (PLEN,sp),a
ldw x,(BUFF,sp)
1$: ld a,(x)
incw x
call uart3_putc
call uart_putc
add a,(CHKSUM,sp)
ld (CHKSUM,sp),a
dec (PLEN,sp)
jrne 1$
ld a,(CHKSUM,sp)
call uart3_putc
call uart_putc
call get_next
cp a,#ACK
jreq 2$
Expand Down

0 comments on commit 592e7b0

Please sign in to comment.