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 > C-64 coding cargo cults
2020-01-14 13:33
Krill

Registered: Apr 2002
Posts: 2980
C-64 coding cargo cults

The most-discussed coding cargo cult on the C-64 is probably SEI/CLI around interrupt setup code.

Here's another one: acknowledging VIC raster interrupts.

According to the datasheet http://archive.6502.org/datasheets/mos_6567_vic_ii_preliminary... an active VIC interrupt is acknowledged by writing a "1" to the corresponding bit in $d019.

The usual way to achieve this seems to be "DEC $D019" and to a lesser extent other read-modify-write instructions, saving a few bytes and/or cycles compared to "LDA $D019 : STA $D019" or "LDA #$xF : STA $D019".
This works because RMW instructions on 6502/6510 read a value (here, the pending interrupts) and write the same value again (clearing the interrupt latches) before writing the modified value.
This is also why this technique does not work on SuperCPU's 65816 in native mode, as its RMW instructions lack the dummy-write of the unmodified value.

Now, the cargo cult bit is this: For raster interrupts, it suffices to write any value with bit 0 set (likewise for other VIC interrupts). Clearing all VIC interrupts can be achieved by writing any value with bits 0..3 set.

So, you can save 2 cycles by simply recycling any register value that happens to have bit 0 set, writing that one to $d019 to acknowledge a VIC raster interrupt.

Please post other coding cargo cults here. =)
 
... 31 posts hidden. Click here to view all posts....
 
2020-01-15 13:49
Oswald

Registered: Apr 2002
Posts: 5094
That makes sense!
2020-01-15 14:25
Krill

Registered: Apr 2002
Posts: 2980
So again: no SEI, no need to ack any pending CIA interrupt, as there will be none after disabling them. =)
2020-01-15 17:28
oziphantom

Registered: Oct 2014
Posts: 490
I mean you expect a guy who counted the number of clocks in a line wrong, off by 1, is going to count the number of pixels a border is.. again off by 1..
2020-01-15 17:32
Frantic

Registered: Mar 2003
Posts: 1648
...and 7 * 9 = 63

I suspect a conspiracy going on here.
2020-01-15 17:43
Golara
Account closed

Registered: Jan 2018
Posts: 212
Quote: I mean you expect a guy who counted the number of clocks in a line wrong, off by 1, is going to count the number of pixels a border is.. again off by 1..

What are you talking about ? The 64 and 65 cycle NTSC machines ?
2020-01-15 20:42
tlr

Registered: Sep 2003
Posts: 1790
LDX #$FF
TXS
perhaps?

Most self contained code will run fine with _any_ initial stack pointer. :)

I do use that construct myself though as I like to keep things tidy.

The question could be interpreted on the edge of making the most minimal code that will still run correctly. I guess most interrupt setup uses SEI/CLI for the intent of making extra sure in some way.
And note that trying to reduce code to the optimal case will require a lot more testing to prove it works. For interrupts you need to test for race conditions. For the SP you'll need to fuzz-test your stack pointer.
2020-01-15 21:39
JackAsser

Registered: Jun 2002
Posts: 2014
Quote:
LDX #$FF
TXS
perhaps?

Most self contained code will run fine with _any_ initial stack pointer. :)

I do use that construct myself though as I like to keep things tidy.

The question could be interpreted on the edge of making the most minimal code that will still run correctly. I guess most interrupt setup uses SEI/CLI for the intent of making extra sure in some way.
And note that trying to reduce code to the optimal case will require a lot more testing to prove it works. For interrupts you need to test for race conditions. For the SP you'll need to fuzz-test your stack pointer.


The extra SEI when taking over the kernel IRQ forces you to do the bit $dc0d though. Leave both out.

Anyway, thanks for Krill's OP. Already employing it:

had:
lda #$00
sta $d017
dec $d019
pla
rti

But since I don't use sprite 0 anyway I changed to:
lda #$1
sta $d017
sta $d019
pla
rti

:D 2 cycles is 2 cycles!
2020-01-15 22:39
Krill

Registered: Apr 2002
Posts: 2980
Quoting tlr
Most self contained code will run fine with _any_ initial stack pointer. :)
Code using the KERNAL's interrupt handler breaks with very low stack pointers, as the BRK check code prior to jumping via $0314/5 is... not good.
http://unusedino.de/ec64/technical/aay/c64/romff48.htm

Quoting tlr
I guess most interrupt setup uses SEI/CLI for the intent of making extra sure in some way.
And note that trying to reduce code to the optimal case will require a lot more testing to prove it works.
I have yet to see a case where no SEI with proper interrupt disabling fails and SEI with all that ack stuff before CLI doesn't. =)
2020-01-16 05:57
oziphantom

Registered: Oct 2014
Posts: 490
Quoting Golara
What are you talking about ? The 64 and 65 cycle NTSC machines ?


Yes
2020-01-16 06:30
ChristopherJam

Registered: Aug 2004
Posts: 1409
While most code doesn't specifically require an initial SP of $ff, it's reasonably common to use some of stack space for data (*cough icc*), in which case you at least need to know *something* about the value you initialise it to.

Of course, that could often just as easily be $00 as $ff, or even some arbitrary value in the middle just so long as you know which half a dozen bytes your JSRs and IRQs will be trampling.
Previous - 1 | 2 | 3 | 4 | 5 - Next
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
Brush/Elysium
Acidchild/Padua
Shogoon/Elysium/MSL
rexbeng
Knight Rider/TREX
Guests online: 129
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 Layers  (9.6)
2 No Listen  (9.6)
3 Party Elk 2  (9.6)
4 Cubic Dream  (9.6)
5 Copper Booze  (9.6)
6 Rainbow Connection  (9.5)
7 Dawnfall V1.1  (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 Musicians
1 Rob Hubbard  (9.7)
2 Mutetus  (9.7)
3 Jeroen Tel  (9.7)
4 Linus  (9.6)
5 Stinsen  (9.6)

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