Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > How can this code work???
2012-01-03 22:57
Shadow
Account closed

Registered: Apr 2002
Posts: 355
How can this code work???

I'm using a bit of FPP-code I wrote a while ago. It seems to work like a charm, but now when I look at it, I can't figure out why it is, because if I count correctly, my code per line is 20 cycles, while a badline should be 23, correct?
This means that my code should be badly out of sync after some lines, but somehow it seems to work!

	lda #$32
!wait:
	cmp $d012
	bne !wait-
	ldy #$03
!delay:
	dey
	bne !delay-
.for(var i=0;i<200;i++)
{
	lda #$18+[[i+$32]&7]
	sta $d011
	ldy fpptable+i
	lda fpporig,y
	sta $d018
	nop
}


(both fpptable and fpptable are page-aligned, non-ZP memory locations. I have no sprites enabled)
2012-01-04 01:00
Fresh

Registered: Jan 2005
Posts: 101
Short answer: BA before AEC.
Long answer:
As soon as $D011 gets the right value the VIC puts BA low and waits 3 cycles to use the buses.
These 3 cycles can only be used for WRITING, they have been put for the cpu to end a possibly long rmw instruction (INC, DEC etc..).
In your case the first thing the cpu does (with BA low) is READING next instruction opcode (LDY): this freezes the processor, that's why you 'lose' three more cycles.
This behavior, while sensible (bus accesses must be tidy), is one of the worst nightmares of all c64 coders.
2012-01-04 09:48
Cruzer

Registered: Dec 2001
Posts: 1048
Another maybe more "common sense" answer... If you have a routine that uses 23 cycles to create badlines each line it produces normal FLI with a 3 char bug. If you use more cycles, the bug is moved right, and if you use less it's moved left, "under the border" and the gfx starts stretching.
2012-01-04 15:27
Skate

Registered: Jul 2003
Posts: 494
@Cruzer: Nice and clear explanation but I think this part can be confusing for some ppl.

"If you use more cycles, the bug is moved right, and if you use less it's moved left"

You don't mean spending more/less cycles each line, just the beginning of the interrupt, right? Since Shadow was talking about 20 available cycles, this part might be understood incorrectly.
2012-01-04 19:18
ChristopherJam

Registered: Aug 2004
Posts: 1409
nah, he means more or less per line. VIC will consume as many cycles are left over, but you need to occupy at least 23 to avoid stretching.
2012-01-04 19:41
Cruzer

Registered: Dec 2001
Posts: 1048
What Christopher said. That's the nice thing about FLI - it's auto-jittercorrecting. For each cycle extra you use, the VIC will just use one less for badline DMA fetching, and vice-versa. For FPP without a FLI bug it needs 40 for DMA + 3 for waiting, so that's why you only have 64-40-3 = 20 left for the code each line.
2012-01-04 20:40
Frantic

Registered: Mar 2003
Posts: 1648
...which should obviously be read as 63-40-3 = 20. It would have been so much nicer if a raster line was actually 64 cycles on the C64. Damnit! :)
2012-01-04 21:13
Cruzer

Registered: Dec 2001
Posts: 1048
Oops! :)
2012-01-04 22:32
Digger

Registered: Mar 2005
Posts: 437
Hah, that's why, I was never quite too sure where all these missing cycles were gone... :)
2012-01-05 16:01
Radiant

Registered: Sep 2004
Posts: 639
Am I the only one finding Freshness79's explaination simple to follow and the "common sense answer" impossible to understand?
2012-01-05 17:23
Skate

Registered: Jul 2003
Posts: 494
@radiantx: Freshness79's explaination was correct but wasn't the answer why FPP-code works in 20 cycles and not in 23 cycles. he explained why by using 20 cycles we still have the correct timing alignment. Cruzer explained why FPP occurs in less then 23 cycles. So two answers complete each other i guess.

I admit that I usually add/remove a few cycles to catch the effect i'm looking for instead of counting cycles. :)
2012-01-05 19:29
tlr

Registered: Sep 2003
Posts: 1790
Quote: ...which should obviously be read as 63-40-3 = 20. It would have been so much nicer if a raster line was actually 64 cycles on the C64. Damnit! :)

Old NTSC? ;)
2012-01-06 06:05
Frantic

Registered: Mar 2003
Posts: 1648
@tlr: mpfngghh... I thought you were my PAL. ;) Anyway, in that case the rest of the calculation would be wrong. ;)
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
macx
CopAss/Leader
Bob/Censor Design
Ben Breton/Absence o..
Acidchild/Padua
ΛΛdZ
Bieno/Commodore Plus
algorithm
E$G/HF ⭐ 7
zscs
MWR/Visdom
Visage/Lethargy
Guests online: 114
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 Edge of Disgrace  (9.6)
6 What Is The Matrix 2  (9.6)
7 The Demo Coder  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 No Listen  (9.6)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 Dawnfall V1.1  (9.5)
7 Rainbow Connection  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Triad  (9.3)
5 Censor Design  (9.3)
Top Swappers
1 Derbyshire Ram  (10)
2 Jerry  (9.8)
3 Violator  (9.7)
4 Acidchild  (9.7)
5 Cash  (9.6)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.118 sec.