Advertisement
Zeda

csrand_v2

Nov 17th, 2022 (edited)
2,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. csrand_init:
  2. ; input: n is uint24 on the stack, 1 <= n <= 256
  3. ;        n * 4 is the samples per bit
  4.     pop hl
  5.     pop de
  6.     ld a,e
  7.     ld (.smc_samples), a
  8.     push de
  9.     push hl
  10.  
  11.     push ix
  12.         ld ix,0
  13.         ld a,0x46
  14. .bit_loop:
  15.         ld (.smc1),a
  16.         ld (.smc2),a
  17.         ld (.smc3),a
  18.         ld (.smc4),a
  19.         push af
  20.             push hl
  21.                 ld bc,513
  22. .scan_loop:
  23.                 push hl
  24.                     call _test_bit
  25.                     ; or a,a
  26.                     sbc hl,de
  27.                     jq nc,.skip1
  28.                     add hl,de
  29.                     ex de,hl
  30.                     pop ix
  31.                     push ix
  32. .skip1:
  33.                 pop hl
  34.                 jq z,.early_exit
  35.  
  36.                 cpi
  37.                 jp pe,.scan_loop
  38.             pop hl
  39.         pop af
  40.         add a,8
  41.         cp 0x86
  42.         jq nz,.bit_loop
  43.         jq .return
  44. .early_exit:
  45.         pop hl
  46.         pop af
  47. .return:
  48.         lea hl, ix+0
  49.         ld (_sprng_read_addr), hl
  50.  
  51.         xor a, a
  52.         sbc hl, bc  ; subtract 0 to set the z flag if HL is 0
  53.     pop ix
  54.     ret z
  55.     inc a
  56.     ret
  57.  
  58. .test_bit:
  59. ; inputs: hl = byte
  60. ; outputs: hl = hit count
  61. ; outputs: carry flag reset
  62. ; destroys: af, bc, de, hl
  63. .smc_samples:=$+1
  64.     ld b,0
  65.     ld de,0
  66. .loop:
  67.     bit 0,(hl)
  68. .smc1:=$-1
  69.     jq z,.next1
  70.     inc de
  71. .next1:
  72.     bit 0,(hl)
  73. .smc2:=$-1
  74.     jq nz,.next2    ; notice the inverted logic !
  75.     dec de          ; and the dec instead of inc !
  76. .next2:
  77.     bit 0,(hl)
  78. .smc3:=$-1
  79.     jq z, .next3
  80.     inc de
  81. .next3:
  82.     bit 0,(hl)
  83. .smc4:=$-1
  84.     jq nz,.next4    ; notice the inverted logic !
  85.     dec de          ; and the dec instead of inc !
  86. .next4:
  87.     djnz .loop
  88.  
  89.     ; return |DE|
  90.     or a,a
  91.     sbc hl,hl
  92.     sbc hl,de
  93.     ret nc
  94.     ex de,hl
  95.     or a, a  ; return with carry reset
  96.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement