;-----------------------------------------------------------------------
; Shift register (31-bit) pseudorandom (white) noise generator
;-----------------------------------------------------------------------
%TITLE "NOISE31.ASM"
IDEAL
DOSSEG
MODEL tiny
DATASEG
shift_reg dd 5a5aa55ah ;a nice seed
speaker_off db 0
speaker_on db 0
welcome db 0ah,0dh
db ' NOISE31 - A "white" noise generator based on a 31-bit shift'
db ' register',0dh,0ah
db ' (as suggested by Don Lancaster).',0ah,0dh,0ah,0dh
db 0ah,0dh,' - ----------------------------- -'
db 0ah,0dh,' - Done For Fun by: -'
db 0ah,0dh,' - xxxxxxxxxxxxxxxxxxxxxxxx -'
db 0ah,0dh,' - xxxxxxxxxxxxxxxxxxxxxxxx -'
db 0ah,0dh,' - xxxxxxxxxxxxxxxxxxxxxxxx -'
db 0ah,0dh,' - ----------------------------- -'
db 0ah,0dh,0ah,0dh
db ' (Press any key to Exit)',0ah,0dh
db '$'
CODESEG
ORG 100h
start:
mov ax,02h ;clear the screen
int 10h
mov dx,offset welcome ;display message
mov ah,09h
int 21h
;get speaker_off setting
mov dx,61h ;speaker port
in al,dx
and al,0fch
mov [speaker_off],al ;save speaker off
out dx,al ;start it off
or al,02h
mov [speaker_on],al
gen_num:
;Determine the result of (bit three xor bit six) of the high
; byte of the shift register and put the result in the carry.
mov bx,[WORD HIGH shift_reg]
and bh,048h ;08h or 40h is result=1 else 0
cmp bh,08h
jz xor1
cmp bh,40h
jz xor1
clc ;xor result=0 so clear carry
mov al,[speaker_off] ;and set speaker byte
jmp short shift_bits
xor1:
stc ;xor result=1 so set carry
mov al,[speaker_on] ;and set speaker byte
;Rotate the shift register with the cf becoming the lsb.
shift_bits:
rcl [WORD LOW shift_reg],1
rcl [WORD HIGH shift_reg],1
;The cf has our random bit but al has our speaker out byte.
;toggle speaker
out dx,al ;out the speaker
;check for a key press to end
@@10:
mov ah,1
int 16h
jz short gen_num ;no press
mov ah,0
int 16h ;trash press
mov ah,4ch
int 21h ;-->DOS
END start