Advertisement
Zeda

csrand

Nov 16th, 2022 (edited)
2,112
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Z80 Assembler 2.22 KB | Source Code | 0 0
  1. csrand_init:
  2. ; ix = selected byte
  3. ; de = current deviation
  4. ; hl = starting address
  5. ; inputs: stack = samples / 4, Max is 256 (256*4 = 1024 samples)
  6. ; outputs: hl = address
  7.     pop hl
  8.     pop de
  9.     ld a,e
  10.     ld (_smc_samples), a
  11.     push de
  12.     push hl
  13.  
  14.     push ix
  15.         ld ix, 0
  16.         ld hl, $D65800
  17.         ld bc,513
  18. .test_range_loop:
  19.         push bc
  20.             call _test_byte
  21.         pop bc
  22.         cpi
  23.         jp pe,.test_range_loop
  24.  
  25.         lea hl, ix+0
  26.         ld (_sprng_read_addr), hl
  27.  
  28.         xor a, a
  29.         sbc hl, bc  ; subtract 0 to set the z flag if HL is 0
  30.     pop ix
  31.     ret z
  32.     inc a
  33.     ret
  34.  
  35. _test_byte:
  36. ; inputs: hl = byte
  37. ; inputs: de = minimum deviance
  38. ; inputs: ix = pointer to the byte with minimum deviance
  39. ; outputs: de is the new minimum deviance (if updated)
  40. ; outputs: ix updated to hl if this byte contains the bit with lowest deviance
  41. ; outputs: b = 0
  42. ; outputs: a = 0x86
  43. ; destroys: f
  44. ; modifies: a, b, de, ix
  45.     ld a,0x46 ; second half of the `bit 0,(hl)` command
  46. .test_byte_bitloop:
  47.     push hl
  48.         push de
  49.             call _test_bit  ; HL = deviance (|desired - actual|)
  50.         pop de
  51.  
  52.         add a,8       ; never overflows, so resets carry
  53.         sbc hl, de    ; check if HL is smaller than DE
  54.  
  55.         jq nc, .skip_next_bit          ; HL >= DE
  56.         add hl,de
  57.         ex de,hl
  58.         pop ix
  59.         push ix
  60. .skip_next_bit:
  61.     pop hl
  62.     cp 0x86
  63.     jq nz, .test_byte_bitloop
  64.     ret
  65.  
  66. _test_bit:
  67. ; inputs: a = second byte of CB**
  68. ; inputs: hl = byte
  69. ; outputs: hl = hit count
  70. ; destroys: af, bc, de, hl
  71.  
  72. _smc_samples:=$+1
  73.     ld b,0
  74.     ld (.smc1),a
  75.     ld (.smc2),a
  76.     ld (.smc3),a
  77.     ld (.smc4),a
  78.     ld de,0
  79. .loop:
  80.     bit 0,(hl)
  81. .smc1:=$-1
  82.     jq z,.next1
  83.     inc de
  84. .next1:
  85.     bit 0,(hl)
  86. .smc2:=$-1
  87.     jq nz,.next2    ; notice the inverted logic !
  88.     dec de          ; and the dec instead of inc !
  89. .next2:
  90.     bit 0,(hl)
  91. .smc3:=$-1
  92.     jq z, .next3
  93.     inc de
  94. .next3:
  95.     bit 0,(hl)
  96. .smc4:=$-1
  97.     jq nz,.next4    ; notice the inverted logic !
  98.     dec de          ; and the dec instead of inc !
  99. .next4:
  100.     djnz .loop
  101.  
  102.     ; return |DE|
  103.     or a,a
  104.     sbc hl,hl
  105.     sbc hl,de
  106.     ret nc
  107.     ex de,hl
  108.     ret
  109.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement