Page 1 of 1
C64: Video noise... and pseudo random number generators.
Posted: Mon Mar 20, 2017 3:29 pm
by ttlworks
Video noise... and pseudo random number generators.
Wanted to share an old but useless little gimmick:

- c64_video_noise.jpg (18.97 KiB) Viewed 2065 times
192*96 pixels, black\white, ca. 20 frames per second, generated on a C64.
;---
While spending two months without internet because of a broken arm,
I eventually found the time to scrap my collection of ca. 400 3½-inch floppy disks.
One of the disk labels remembered me back to the time when we did some 80x86 assembly
coding in a "DOS environment" at tech school... back in 1997. As time goes by.
At some point, I had felt a little bit bored, and just for the fun of it I had tried
to generate something that looks a little bit like video noise on the PC screen
with 48 Bytes of machine code or such.
Luckily, I still had a version of that 20 years old code printed on paper,
and because I recently felt bored again, I couldn't resist to try an implementation on the C64.
Just to make things clear:
I never tried to write any 80x86 assembly code after 1997,
and I would like to keep it that way ! 
Re: C64: Video noise... and pseudo random number generators.
Posted: Mon Mar 20, 2017 3:30 pm
by ttlworks
Now my old code for a DOS PC.
With a Win XP PC from 2010, the code still worked in a DOS box set to full screen mode.
I'm not sure if it still works for newer implementations of the PC... or Windows.
Code: Select all
; MASM x86 DOS6.22 1997
STAPEL SEGMENT STACK
DW 64 DUP (?)
STAPEL ENDS
HAUPT SEGMENT
ASSUME CS:HAUPT;SS:STAPEL
mov ax,0006h ;Video mode 640*200 graphics black\white
int 10h
mov ax,0b800h ;set segments
mov ds,ax
mov es,ax
mov ds:[0],ax ;"seed" for random generator
cld ;direction for stosw, not to get confused with 6502 CLD.
RAUSCH: xor di,di ;set pointers
mov cx,2000h
RAU1: mov ax,ds:[di]
adc bx,ax ;"randomly" change pixels
adc ax,bx
stosw
loop RAU1 ;screen completed ?
mov ah,2 ;read keyboard status
int 16h
and al,0fh ;CTRL,SHIFT or ALT pressed ?
jz RAUSCH ;exit, if yes.
mov ax,0003h ;Video mode 80*25 text
int 10h
mov ah,4ch
int 21h ;end program.
;
HAUPT ENDS
END
Edit: fixed typo.
Re: C64: Video noise... and pseudo random number generators.
Posted: Mon Mar 20, 2017 3:33 pm
by ttlworks
Now for my attempt of making a C64 implementation.
Code would have been simpler when generating a 320*200 picture.
Problem is, that the speed breaks down to not much more than 6 frames per second
when the C64 has to generate a picture that big.
But the optical effect on the screen won't look nice for less than 20 frames
per second, so I decided to use a smaller picture for increasing speed.
Code: Select all
;Quick & dirty C64 implementation
;only 192*96 pixels used
INIT
LDA #$00 ; background color
STA $D020
STA $D021
LDA $3B ; 320*200 HiRes mode
STA $D011
LDA $18
STA $D018
;
LDX #$04 ; init color RAM
LDA #$00
JSR CLR
LDX #$20 ; init graphics RAM
LDA #$10
JSR CLR
;
NOISE
LDA #$7F ; init pointer
STA $22
LDA #$26
STA $23
LDX #$0C
NO1
LDY #$C0
NO2
ROR A ; the inner loop
ADC ($22),
ROL A
STA ($22),Y
DEY
BNE NO2
PHP
PHA
CLC
LDA $22
ADC #$40
STA $22
LDA $23
ADC #$01
STA $23
PLA
PLP
DEX
BPL NO1
LDY $91 ;Run Stop ?
BMI NOISE
JMP ($FF81) ;reset VIC2 and exit, if yes.
;----------------------------
CLR
LDY #$00
STY $22
STX $23
CLR1
STA ($22),Y
INY
BNE CLR1
INC $23
DEX
BNE CLR1
RTS
;=============================
.1000 A9 00 8D 20 D0 8D 21 D0
.1008 A9 3B 8D 11 D0 A9 18 8D
.1010 18 D0 A2 04 A9 10 20 50
.1018 10 A2 20 A9 00 20 50 10
.1020 A9 7F 85 22 A9 26 85 23
.1028 A2 0C A0 C0 6A 71 22 2A
.1030 91 22 88 D0 F7 08 48 18
.1038 A5 22 69 40 85 22 A5 23
.1040 69 01 85 23 68 28 CA 10
.1048 E1 A4 91 30 D3 6C 81 FF
.1050 A0 00 84 22 86 23 91 22
.1058 C8 D0 FB E6 23 CA D0 F6
.1060 60
That's all, have fun with tinkering.
BTW: I'm not sure if the 6502 assembly listing is without errors,
simply because it manually was generated from the raw machine code above.
Re: C64: Video noise... and pseudo random number generators.
Posted: Mon Mar 20, 2017 4:52 pm
by Dr Jefyll
While spending two months without internet because of a broken arm,
I eventually found the time to scrap my collection of ca. 400 3½-inch floppy disks.
I, too, have a collection of old floppies which needs to be sorted through. But I hope to accomplish this without first breaking my arm!
Welcome back, Dieter! Glad to hear you're okay!
-- Jeff
Re: C64: Video noise... and pseudo random number generators.
Posted: Mon Mar 20, 2017 6:46 pm
by BigDumbDinosaur
I, too, have a collection of old floppies which needs to be sorted through. But I hope to accomplish this without first breaking my arm!

I never knew that old floppies were so heavy.

Re: C64: Video noise... and pseudo random number generators.
Posted: Mon Mar 20, 2017 7:16 pm
by floobydust
Well, I weighed a group of 10- 3.5-inch floppies, 212 grams, or about 21.2 grams per floppy. Lifting 400 of them would be 8480 grams, or 8.48Kg or about 18.66 pounds. Unless his count is off by a lot, I don't really see any broken bones

Re: C64: Video noise... and pseudo random number generators.
Posted: Mon Mar 20, 2017 7:24 pm
by BigDumbDinosaur
Well, I weighed a group of 10- 3.5-inch floppies, 212 grams, or about 21.2 grams per floppy. Lifting 400 of them would be 8480 grams, or 8.48Kg or about 18.66 pounds.
It figures that someone would figure out
tedious minutia such as how much a pile of floppy disks would weigh.
Unless his count is off by a lot, I don't really see any broken bones

I'd be more concerned about having 400 floppies piled so high the ceiling fan is batting them around the house.

More likely, Dieter's wife was upset with him and smacked him in the arm with a cast iron skillet.

Re: C64: Video noise... and pseudo random number generators.
Posted: Mon Mar 20, 2017 7:44 pm
by floobydust
Well, I weighed a group of 10- 3.5-inch floppies, 212 grams, or about 21.2 grams per floppy. Lifting 400 of them would be 8480 grams, or 8.48Kg or about 18.66 pounds.
It figures that someone would figure out
tedious minutia such as how much a pile of floppy disks would weigh.
Unless his count is off by a lot, I don't really see any broken bones

I'd be more concerned about having 400 floppies piled so high the ceiling fan is batting them around the house.

More likely, Dieter's wife was upset with him and smacked him in the arm with a cast iron skillet.

Haha... well, you sorta asked for this one:
Said stack of 10 floppies are about 33.4mm thick, so 400 of them would be about 1.336 meters high, or 52.6 inches... bottom line, he's going to need a lot more floppies before they hit the fan!
Also, hardly tedious... took barely a minute!
Re: C64: Video noise... and pseudo random number generators.
Posted: Mon Mar 20, 2017 9:24 pm
by BigDumbDinosaur
Also, hardly tedious... took barely a minute!
Glad someone has time to seek out these obscure bits of information.

Re: C64: Video noise... and pseudo random number generators.
Posted: Tue Mar 21, 2017 11:10 am
by John West
192x96 is the same number of pixels as a 24x12 block of characters, which is 288 of them. 256 isn't much lower. So... fill the screen with 256 different characters in some random pattern (you don't want any regularity in the layout, but you also don't want the duplicates of one character to occur too close together) and fill their definitions with your random numbers. Voila! A full screen of 20Hz snow.
Re: C64: Video noise... and pseudo random number generators.
Posted: Tue Mar 21, 2017 10:23 pm
by commodorejohn
192x96 is the same number of pixels as a 24x12 block of characters, which is 288 of them. 256 isn't much lower. So... fill the screen with 256 different characters in some random pattern (you don't want any regularity in the layout, but you also don't want the duplicates of one character to occur too close together) and fill their definitions with your random numbers. Voila! A full screen of 20Hz snow.
Yeah, that was my thought. Lay out the character map in some pattern that's irregular enough to not break the effect, and you can generate a much smaller amount of noise and still give essentially the same effect, for purposes of human perception.
Re: C64: Video noise... and pseudo random number generators.
Posted: Fri Mar 24, 2017 12:10 am
by repose
That's very fast for what you're doing. I hate to say this, but I've seen demos with video noise, and I'm pretty sure they just set the sound chip to produce noise and read the waveform register. You can do this in realtime and fill most of the screen with noise.
The sid chip does a 23 bit lfsr generator in hardware.
http://www.oxyron.de/html/registers_sid.html
Re: C64: Video noise... and pseudo random number generators.
Posted: Fri Mar 24, 2017 8:06 am
by ttlworks
@Jeff: thanks for the warm welcome. I'm still a bit slow with typing... might be getting better in half a year or so.
@repose: when spending some more thoughts on this, I'd say you are right about that using the
SID LFSR would be a better approach for generating noise on the screen.
Thanks for telling me.
Anyhow, out of sentimental reasons I just wanted to know how that old piece of x86 code performs when getting ported to a ca. 1MHz 6502.
I somehow just never found the time to make a try during the past 20 years.
