?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;
  3. ;Arithmetic operations:
  4. ;
  5. ;neg* : Calculate 2's complement of a 16-bit register
  6. ;abs* : Ensure a 16-bit register is +ve
  7. ;
  8. abshl:  bit     7,h
  9.         ret     z
  10. neghl:  push    af
  11.         ld      a,h     ;To ones complement
  12.         cpl
  13.         ld      h,a
  14.         ld      a,l
  15.         cpl
  16.         ld      l,a
  17.         inc     hl      ;To twos complement
  18.         pop     af
  19.         ret
  20. ;
  21. absde:  bit     7,d
  22.         ret     z
  23. negde:  push    af
  24.         ld      a,d     ;To ones complement
  25.         cpl
  26.         ld      d,a
  27.         ld      a,e
  28.         cpl
  29.         ld      e,a
  30.         inc     de      ;To twos complement
  31.         pop     af
  32.         ret
  33. ;
  34. absbc:  bit     7,b
  35.         ret     z
  36. negbc:  push    af
  37.         ld      a,b     ;To ones complement
  38.         cpl
  39.         ld      b,a
  40.         ld      a,c
  41.         cpl
  42.         ld      c,a
  43.         inc     bc      ;To twos complement
  44.         pop     af
  45.         ret
  46. ;
  47. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  48. ;
  49. ;Signed 16-bit comparison
  50. ;
  51. cpsign:
  52. ;
  53. ;IF HL < DE, return Carry set. IF HL >= DE, return Carry clear. IF HL=DE
  54. ;return Zero set.
  55. ;
  56. ;Check if one is -ve and the other positive
  57. ;
  58.         ld      a,h
  59.         xor     d       ;IF the sign bits were different, bit 7 of A is set
  60.         bit     7,a
  61.         jr      nz,dsign
  62. ;
  63. ;Signs are the same.
  64. ;
  65. ; << v1.01 Do not reverse the comparison if both values are negative.
  66. ;         (In 2s-complement, 0FFFFh (-1) > 0FFFDh (-3) )
  67. ;
  68. ;        So just fall through to the unsigned comparison.
  69. ; >>
  70. ;
  71. cphlde:
  72. cpusgn: ld      a,h
  73.         cp      d
  74.         ret     nz
  75.         ld      a,l
  76.         cp      e
  77.         ret
  78. ;
  79. cpdebc: ld      a,d
  80.         cp      b
  81.         ret     nz
  82.         ld      a,e
  83.         cp      c
  84.         ret
  85. ;
  86. dsign:  bit     7,h     ;Is HL the negative one?
  87.         scf
  88.         ret     nz      ;IF so, HL < DE. Return NZ C
  89. ncnz:   xor     a
  90.         inc     a       ;Force NZ
  91.         ret             ;Otherwise, HL >= DE. Return NZ NC
  92. ;
  93. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  94. ;
  95. ;Signed 16-bit multiplication, BC * DE -> DE
  96. ;
  97. mult16: push    af
  98.         push    bc
  99.         push    hl
  100.         call    m16w
  101.         pop     hl
  102.         pop     bc
  103.         pop     af
  104.         ret
  105. ;
  106. m16w:   ld      a,b
  107.         xor     d
  108.         bit     7,a     ;Negative * positive
  109.         jr      nz,mixmult
  110.         call    absde
  111.         call    absbc
  112.         jr      umult16
  113. ;
  114. mixmult:
  115.         call    absde
  116.         call    absbc
  117.         call    umult16
  118.         jp      negde
  119. ;
  120. ;Unsigned 16-bit multiplication, HLDE := BC * DE
  121. ;
  122. umult16:
  123.         ld      hl,0
  124.         ld      a,16    ;16-bit multiplication
  125. umulta: bit     0,e
  126.         jr      z,umultb
  127.         add     hl,bc
  128. umultb: srl     h
  129.         rr      l
  130.         rr      d
  131.         rr      e
  132.         dec     a
  133.         jr      nz,umulta
  134.         ret
  135. ;
  136. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  137. ;
  138. ;Compute BC mod DE, returns result in HL.
  139. ;
  140. smod16: push    de
  141.         push    bc
  142.         call    mo16w
  143.         pop     bc
  144.         pop     de
  145.         ret
  146. ;
  147. mo16w:  call    absde   ;a mod (-b) == a mod b
  148.         bit     7,b     ;(-a) mod b == - (a mod b)
  149.         jr      z,udiv16
  150.         call    absbc
  151.         call    udiv16
  152.         jp      neghl
  153.  
  154. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  155. ;
  156. ;Divide BC by DE, returns result in BC.
  157. ;
  158. sdiv16: push    de
  159.         push    hl
  160.         call    d16w
  161.         pop     hl
  162.         pop     de
  163.         ret
  164. ;
  165. d16w:   ld      a,b             ;Same sign, or opposite signs?
  166.         xor     d
  167.         bit     7,a
  168.         jr      nz,mixdiv
  169.         call    absde
  170.         call    absbc
  171.         jr      udiv16
  172.  
  173. mixdiv: call    absde
  174.         call    absbc
  175.         call    udiv16
  176.         ret     nc
  177.         call    negbc
  178.         scf
  179.         ret
  180. ;
  181. udiv16: ld      a,d     ;Divides BC by DE. Gives result in BC, remainder in HL.
  182.         or      e
  183.         ret     z       ;Return NC if dividing by 0
  184.         ld      hl,0
  185.         ld      a,16
  186. udiv1:  scf
  187.         rl      c
  188.         rl      b
  189.         adc     hl,hl
  190.         sbc     hl,de
  191.         jr      nc,udiv2
  192.         add     hl,de
  193.         dec     c
  194. udiv2:  dec     a
  195.         jr      nz,udiv1
  196.         scf
  197.         ret
  198. ;
  199. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  200. ;<< v0.03 overhauled
  201. ;
  202. ;
  203. ;Random number generator...
  204. ;
  205. rmode:  defb    0       ;0: pseudo-random  1:counting 1-n
  206. rseed:  defw    0
  207. rlast:  defw    0
  208. ;
  209. ;Seed with a randomish number
  210. ;
  211. srandr: call    ZXRNDI  ;Get some randomish number
  212.         ld      a,r
  213.         xor     d
  214.         ld      d,a     ;DE = randomish number
  215.         ld      (rseed),de
  216.         xor     a
  217.         ld      (rmode),a
  218.         ld      (rlast),de
  219.         ret
  220. ;
  221. ;Seed with a fixed number (-HL)
  222. ;
  223. srand:  call    abshl
  224.         ld      (rseed),hl
  225.         ld      de,1000
  226.         call    cphlde
  227.         ld      a,1
  228.         jr      c,srand1
  229.         inc     a
  230. srand1: ld      (rmode),a
  231.         ld      hl,0
  232.         ld      (rlast),hl
  233.         ret
  234. ;
  235. random: bit     7,h             ;IF HL negative, set predictable mode
  236.         jr      nz,srand        ;
  237.         ld      a,h             ;IF HL 0, seed from random number
  238.         or      l
  239.         jr      z,srandr
  240.         ld      a,(rmode)       ;Mode 1: Generate random number by stepping
  241.         dec     a
  242.         jr      z,steprnd
  243. ;
  244. ;Conventional pseudo-random-number generator
  245. ;
  246. ;Using the algorithm: ADD 1; multiply by 75; extract MOD 65537; subtract 1
  247. ;                     and 32-bit arithmetic
  248. ;
  249.         push    hl              ;HL = max value (0 < result <= HL)
  250.         ld      hl,(rseed)
  251. pred0:  ld      de,0            ;DEHL = value
  252. ;
  253. ;ADD 1 to DEHL.
  254. ;
  255.         inc     hl
  256.         ld      a,h
  257.         or      l
  258.         jr      nz,pred1
  259.         inc     de
  260. pred1:
  261. ;
  262. ;Multiply by 75. 75 = 64+8+2+1...
  263. ;
  264.         push    de
  265.         push    hl      ;1xDEHL
  266.         call    dblhde  ;*2
  267.         push    de
  268.         push    hl      ;2xDEHL
  269.         call    dblhde  ;*4
  270.         call    dblhde  ;*8
  271.         push    de
  272.         push    hl      ;8xDEHL
  273.         call    dblhde  ;*16
  274.         call    dblhde  ;*32
  275.         call    dblhde  ;*64
  276.         call    addpop  ;+X*8 = X*72
  277.         call    addpop  ;+X*2 = X*74
  278.         call    addpop  ;+X   = X*75
  279. ;
  280. ;Extract modulo 65537 (0x10001).
  281. ;
  282. modlp:  ld      a,e
  283.         cp      1
  284.         jr      c,end655        ;IF it's under 0x10000, it's under 0x10001
  285.         jr      nz,sub655       ;IF it's above 0x1FFFF, it's above 0x10001
  286.         ld      a,h             ;0x10000 <= DEHL <= 0x1FFFF
  287.         or      a
  288.         jr      nz,sub655       ;IF it's above 0x100FF, it's above 0x10001
  289.         ld      a,l             ;0x10000 <= DEHL <= 0x100FF
  290.         cp      2
  291.         jr      c,end655        ;IF it's <= 0x10001, return
  292. ;
  293. ;Subtract 65537 from DEHL...
  294. ;
  295. sub655: dec     de
  296.         dec     hl
  297.         jr      modlp
  298. ;
  299. end655:
  300. ;
  301. ;Subtract 1
  302. ;
  303.         dec     hl
  304.         ld      (rseed),hl
  305.         ld      b,h
  306.         ld      c,l
  307. ;
  308. ;BC = number (16-bit). Reduce modulo parameter
  309. ;
  310.         res     7,b
  311.         pop     de
  312.         call    smod16
  313.         inc     hl              ;<<v0.03>> value is 0 < n <= max
  314.                                 ;          not      0 <=n < max
  315.         scf
  316.         ret                     ;Return value in HL.
  317.  
  318.  
  319.  
  320. steprnd:
  321.         push    hl
  322.         ld      bc,(rlast)      ;Stepping 1,2, ... seed
  323.         inc     bc
  324. ;
  325. ;BC = number candidate
  326. ;
  327.         ld      de,(rseed)
  328.         call    smod16          ;BC := BC mod (seed)
  329.         ld      (rlast),hl
  330.         pop     de
  331.         ld      b,h
  332.         ld      c,l
  333.         res     7,b
  334.         call    smod16          ;Reduce mod parameter
  335.         inc     hl              ;0 < x <= param
  336.         scf
  337.         ret
  338. ;
  339. ;Double DEHL
  340. ;
  341. dblhde: sla     l
  342.         rl      h
  343.         rl      e
  344.         rl      d
  345.         ret
  346. ;
  347. addpop: pop     bc      ;return address
  348.         ld      (apret+1),bc
  349.         pop     bc      ;To add to the low word
  350.         add     hl,bc
  351.         pop     bc      ;To add to the high word
  352.         ex      de,hl
  353.         adc     hl,bc
  354.         ex      de,hl
  355. apret:  jp      0
  356. ;
  357.  
  358. ; >> v0.03
  359.