6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun Nov 24, 2024 9:27 am

All times are UTC




Post new topic Reply to topic  [ 29 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: Fri Mar 07, 2008 5:44 pm 
Offline

Joined: Sat Jan 04, 2003 10:03 pm
Posts: 1706
From Lambda the Ultimate blog: http://www.uni-koblenz.de/~laemmel/expression/


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Mar 09, 2008 3:37 pm 
Offline

Joined: Sat Sep 22, 2007 1:31 am
Posts: 24
dclxvi wrote:
I agree. One thing I've done when writing self-modifying code is only make one change at a time from non-self-modifying to self-modifying, then test that change. Then, if it goes off into the weeds and pretty much clobbers everything in RAM (hey, it happens :)), I know where to look, and it's usually not too difficult to spot my error. On a related note, another principle I try to adhere to is don't get too clever too quickly. In other words, if it's really wild, start with something simpler and make small changes. Using those principles, self-modifying code doesn't seem (to me) to be significantly more difficult to debug than other code.


Yeah, I take it in steps as well. I usually write out the code in unoptimized easy to understand version, then rewrite it from scratch for self modifying version. The emu debugger I use is great though and very accurate.


Top
 Profile  
Reply with quote  
 Post subject: Re: Self modifying code
PostPosted: Thu Oct 29, 2015 7:07 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8546
Location: Southern California
I was looking around to see what others have done with self-modifying code, toying with the idea of writing a 6502/816-oriented article on it. [Edit, 6/17/19: Written and posted.] The more I look, the more I think there's a good amount of power available there that most of us have only scratched the surface of. Besides needing to finish up some other things, I need a rest from writing, and I need to get back to building. But if someone else will write a good 6502/816-oriented article on self-modifying code, I would definitely like to link to it on my links page, because this material needs to be clearly documented and available to everyone. It should gather and organize as much knowledge as possible on the subject, and give links and credits. This topic here can be a collection point, although it will lack organization.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: Self modifying code
PostPosted: Thu Oct 29, 2015 1:27 pm 
Offline
User avatar

Joined: Mon May 25, 2015 2:25 pm
Posts: 693
Location: Gillies, Ontario, Canada
Coming from AVR, it's almost ironic to call it "Self Modifying Code" at all. I see the 6502 as being a processor with 256 fast registers and 65280 slow registers!
Code, or data... it doesn't matter. It's just 64K that can be used to accomplish a given task.

On the AVR, writing to the Program Memory does have its consequences, since you are dealing with Flash. Slow write times, wear leveling, etc.

I am just a 6502 newb, but in my fresh opinion, "Self Modifying Code" makes absolute sense when you consider how much optimization it offers - both speed and code size. Is it more complex?... garbage in, garbage out!

Brad


Top
 Profile  
Reply with quote  
 Post subject: Re: Self modifying code
PostPosted: Thu Oct 29, 2015 2:54 pm 
Offline
User avatar

Joined: Tue Mar 02, 2004 8:55 am
Posts: 996
Location: Berkshire, UK
Most of the code I write is intended for ROM so self modifying code is not really applicable but I have used it a few times.

_________________
Andrew Jacobs
6502 & PIC Stuff - http://www.obelisk.me.uk/
Cross-Platform 6502/65C02/65816 Macro Assembler - http://www.obelisk.me.uk/dev65/
Open Source Projects - https://github.com/andrew-jacobs


Top
 Profile  
Reply with quote  
 Post subject: Re: Self modifying code
PostPosted: Thu Oct 29, 2015 2:55 pm 
Offline
User avatar

Joined: Sat Dec 07, 2013 4:32 pm
Posts: 246
Location: The Kettle Moraine
When I was first working with 6502 assembly, I didn't know any better, so, I wondered why anyone would bother using STA ($00),y unless the code was in ROM. I always used STA $0000 and changed the operand bytes to my destination location.

As a matter of principal, I just don't do that anymore.

Once in a while, I think about using it to effectively JSR indirect, but, I never actually end up doing it.


Top
 Profile  
Reply with quote  
 Post subject: Re: Self modifying code
PostPosted: Thu Oct 29, 2015 3:35 pm 
Offline
User avatar

Joined: Sun Dec 29, 2002 8:56 pm
Posts: 460
Location: Canada
One place where I used self-modifying code extensively was in a graphics library for the PC. Rather than have a conditional to determine the raster op, I had the code hardcoding the raster op using self modification. It sped up the graphics enormously (by eliminating all the branches).

_________________
http://www.finitron.ca


Top
 Profile  
Reply with quote  
 Post subject: Re: Self modifying code
PostPosted: Thu Oct 29, 2015 7:46 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8546
Location: Southern California
Rob Finch wrote:
One place where I used self-modifying code extensively was in a graphics library for the PC. Rather than have a conditional to determine the raster op, I had the code hardcoding the raster op using self modification. It sped up the graphics enormously (by eliminating all the branches).

Mike Naberezny did something like that to scroll a window that was only part of the screen on the PET.

KC9UDX wrote:
When I was first working with 6502 assembly, I didn't know any better, so, I wondered why anyone would bother using STA ($00),y unless the code was in ROM. I always used STA $0000 and changed the operand bytes to my destination location.

The CMOS 6502 does have STA(zp), an indirect without the indexing, taking 5 clocks rather than 4 like STA abs.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: Self modifying code
PostPosted: Thu Oct 29, 2015 9:54 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8514
Location: Midwestern USA
Rob Finch wrote:
One place where I used self-modifying code extensively was in a graphics library for the PC. Rather than have a conditional to determine the raster op, I had the code hardcoding the raster op using self modification. It sped up the graphics enormously (by eliminating all the branches).

I've been guilty of using self-modifying code over the years, but am always mindful of the potential booby traps such code can bring. Indeed, modifying an absolute address so as to avoid the time penalty of indirect access is an excellent way to speed up video access. The 65C816 in native mode lessens the value of such a tactic, however, since one can cover a 64K address space with a 16 bit index using something like LDA $00,X.

In the UNIX environment, self-modifying code is not allowed, although it was at one time. An attempt to write into the code segment of a running process causes a memory access violation, followed by a core dump and forced termination of the process.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
 Post subject: Re: Self modifying code
PostPosted: Mon Nov 02, 2015 2:33 am 
Offline

Joined: Tue Jul 24, 2012 2:27 am
Posts: 679
Self-modification can speed up dispatch points, and I don't think that's a problem.

I also have some round-robin code in one of my projects. Each routine gets a pass per frame or per interrupt, depending on whatever's active. The first instruction of the routine is "BIT nextRoutine" when enabled, which gets self-modded to "JMP nextRoutine" when disabled. I do similar in interrupting AcheronVM. Rerouting flow of control is really low overhead this way, as no checks are involved in the standard case. Changing a single instruction opcode costs exactly the same as setting a variable/flag to a particular value.

_________________
WFDis Interactive 6502 Disassembler
AcheronVM: A Reconfigurable 16-bit Virtual CPU for the 6502 Microprocessor


Top
 Profile  
Reply with quote  
 Post subject: Re: Self modifying code
PostPosted: Thu Nov 12, 2015 6:19 am 
Offline

Joined: Tue Feb 19, 2013 12:29 am
Posts: 32
Location: Marlborough, Ma
Why wouldn't you use a macro that takes parameters instead of self modifying code? It is much cleaner and safer.


Top
 Profile  
Reply with quote  
 Post subject: Re: Self modifying code
PostPosted: Thu Nov 12, 2015 6:35 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8514
Location: Midwestern USA
pebmeister wrote:
Why wouldn't you use a macro that takes parameters instead of self modifying code? It is much cleaner and safer.

A macro is an assembly-time function. Self-modifying code is a run-time function.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
 Post subject: Re: Self modifying code
PostPosted: Thu Nov 12, 2015 5:05 pm 
Offline

Joined: Wed Oct 06, 2010 9:05 am
Posts: 95
Location: Palma, Spain
I've used loads of self-modifying code in my time, the target platform being the BBC Micro, running code out of RAM.

One example was a sprite routine which ran in the zero page which self-modified plenty of its own operands (the self-modified address being in zp as well, saving another cycle). Here's a snippet of the innermost bit of the loop:

Code:
innerloop:
read:
    LDA $FFFF,Y    ; self-modified
    STA maskindex+1
maskindex:
    LDA masktable  ; page-aligned table
write:
    AND $FFFF,Y    ; self-modified
    ORA maskindex+1
    STA (write+1),Y
    INY

etc

That reads sprite data (from (read+1)), looks up a mask for it in a pre-prepared table, masks the screen background, ORs the data over the masked screen data, and writes it back. I figure that the self-modification there saves 4 cycles per byte, which for this particular game saved up to 4900 cycles per frame from just that, plus other more modest savings elsewhere - not bad!

I also remember using them to self-modify the branch destination when entering an IRQ service routine, so that consecutive timer interrupts would go to different places in the code without needing to explicitly check it.


Top
 Profile  
Reply with quote  
 Post subject: Re: Self modifying code
PostPosted: Thu Nov 12, 2015 6:04 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1950
Location: Sacramento, CA, USA
That inner loop looks pretty intense, Rich! A self-modifying ISR is a cool idea as well, as long as you stay on top of things and don't get out of sync.

Mike B.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 29 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: John West and 3 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: