nedoPC.org

Electronics hobbyists community established in 2002
Atom Feed | View unanswered posts | View active topics It is currently 16 Apr 2024 09:30



Reply to topic  [ 3 posts ] 
Генератор псевдослучайных чисел RND 
Author Message
God
User avatar

Joined: 03 Feb 2003 13:53
Posts: 1078
Reply with quote
Хотелось бы заиметь сабжевую функцию с возможностью задания нижней и верхней границы:)


14 Mar 2004 04:00
Profile
God
User avatar

Joined: 29 Dec 2003 01:00
Posts: 1101
Location: Москва
Reply with quote
cr0acker wrote:
Хотелось бы заиметь сабжевую функцию с возможностью задания нижней и верхней границы:)


Влад, вот обещанная ссылка.
Тут много полезных примеров!
http://zilog.sh.cvut.cz/~baze/misc/z80bits.html


16 Mar 2004 00:36
Profile ICQ WWW
Junior

Joined: 31 May 2004 19:40
Posts: 2
Location: St. Petersburg
Reply with quote
Post 
Code:
        global mul

; Генератор случайных чисел, медленный ~3000 takts.
; тpебует наличия функции умножения

RAND_MAX equ 0xffff

        psect data
randx:   dw 0, 0

        psect text

; hl = random(0..<de) генерация числа в диапазоне

randn:
        push de
        call rand
        ex de, hl
        pop bc
        call mul
        ex de, hl
        ret

; Получение случайного числа
; -> hl = случайное число 0..65535 ~2300 takts.

rand:
        ; randx = randx*1103515245 + 12345)>>16) & 077777)
        ld de, (randx+2)
        ld bc, 0x4e6d
        call mul
        push hl
        ld de, (randx)
        ld bc, 0x41c6
        call mul
        pop de
        add hl, de
        push hl
        ld de, (randx)
        ld bc, 0x4e6d
        call mul
        ld bc, 0x3039
        add hl, bc
        ld (randx), hl
        pop hl
        adc hl, de
        ld (randx+2), hl
        ret

; Установка генератора случайных чисел
; de = инициирующее значение

srand:
        ld (randx), de
        ret

 EXPORT global mul  ; long (DE,HL) = DE*BC  ~730t.

; функция беззнакового умножения 16-разрядных чисел
; (C) Kirill Frolov
;     idea by Vasil Ivanov
; dehl=de*bc   ~730t.
        psect text
mul:
        ld hl, 0
        ld a, c
        ld c, l    ; 18

        add a, a
        jr nc, $+4
        add hl, de
        adc a, c   ; 29
     ; REPEAT 7
          add hl, hl
          rla
          jr nc, $+4
          add hl, de
          adc a, c   ; 40*7  309
              add hl, hl
              rla
              jr nc, $+4
              add hl, de
              adc a, c
            add hl, hl
            rla
            jr nc, $+4
            add hl, de
            adc a, c
              add hl, hl
              rla
              jr nc, $+4
              add hl, de
              adc a, c
            add hl, hl
            rla
            jr nc, $+4
            add hl, de
            adc a, c
              add hl, hl
              rla
              jr nc, $+4
              add hl, de
              adc a, c
            add hl, hl
            rla
            jr nc, $+4
            add hl, de
            adc a, c
     ; ENDR

        ld c, a
        push hl   ; 0chl
        ld hl, 0
        ld a, b
        ld b, l   ; 33

        add a, a
        jr nc, $+4
        add hl, de
        add a, b   ; 29
     ; REPEAT 7
          add hl, hl
          rla
          jr nc, $+4
          add hl, de
          add a, b   ; 40*7  309
              add hl, hl
              rla
              jr nc, $+4
              add hl, de
              add a, b
            add hl, hl
            rla
            jr nc, $+4
            add hl, de
            add a, b
              add hl, hl
              rla
              jr nc, $+4
              add hl, de
              add a, b
            add hl, hl
            rla
            jr nc, $+4
            add hl, de
            add a, b
              add hl, hl
              rla
              jr nc, $+4
              add hl, de
              add a, b
            add hl, hl
            rla
            jr nc, $+4
            add hl, de
            add a, b
     ; ENDR

   ; ahl0 + 0cde = hlde
        pop de
        ld b, l
        ld l, h
        ld h, a
        ld a, d
        add a, b
        ld d, a
        ld b, 0
        adc hl, bc
        ex de, hl   ; 60

        ret         ; 729t.


31 May 2004 19:50
Profile ICQ
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: Google [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.