English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Asm / Hardware

 
 
Thread Tools
Old 26 May 2021, 23:33   #1
burma-shave
Registered User
 
Join Date: Mar 2021
Location: Seattle / USA
Posts: 17
Question Loading base address of custom hw registers and using offsets

I'm working my way through the Amiga Hardware Reference Manual). I'm new to programming at this level and am a bit perplexed by the style used to access hardware registers. The examples in the manual load the base address of the hardware registers into an address register and then use offsets to access each of the registers.

From Page 10 (3rd edition):
Code:
XREF      _custom


lea       _custom,a0
move.w    #$7FFF,intena(a0)
https://archive.org/details/commodor...age/8/mode/1up

So I'm guessing it was expected that _custom could vary between machines, but even then, it's not being resolved at runtime so I'm not sure what the above style is giving you.
burma-shave is offline  
Old 26 May 2021, 23:44   #2
roondar
Registered User
 
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,408
This is a pretty common style to use on the 68000. It makes sense to use it over a direct address because it's faster (16 vs 20 cycles).

Edit: that was a bit short, so let me add a bit more about why I think this makes sense.
To me, this method makes a lot of sense for another reason: using these offsets means you're closer to how the designers/chips looks at the registers. The $dff000 is just a prefix for the 68000, in actuality the registers are numbered from $000 onwards. You can see this in the HRM where you'll notice that the register tables they give for never include the $dff000, just the part after the $dff. It also fits better with the Copper, which omits the $dff as well.

Last edited by roondar; 26 May 2021 at 23:55. Reason: Added some more of my reasons for liking this way of doing things
roondar is offline  
Old 27 May 2021, 00:25   #3
burma-shave
Registered User
 
Join Date: Mar 2021
Location: Seattle / USA
Posts: 17
Ah, many thanks for the reply! Yes it did look more readable to me, but I was surprised it was faster than
move.w #xxxx,xxx.L
. I think I see it now though. I don't understand it fully, but d16(An) is more compact than specifying a 32 bit address.
burma-shave is offline  
Old 27 May 2021, 09:41   #4
roondar
Registered User
 
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,408
Yes, d16(An) is more compact than specifying a 32 bit address and that is why it's faster.

It all has to do with how instructions are encoded by the 68000 (and up). The 68000 series uses single words for instructions, with optionally one or more extension words. This means that the move instruction (in it's many, many forms) is encoded as a word for the base instruction and optionally one or more extra words for any extra information needed. Each word the 68000 fetches from memory adds at least 4 cycles* to the cost of the instructions (and, obviously, 2 bytes to the program size).

In case of a d16(An), the d16 does not fit in the first word along with the move instruction because it can be 16 full bits on it's own, so it's put in a separate word. The same goes for a xxx.L: those 32 full bits can't be put alongside the instruction in the first word, so have to be stored separately. The result is that move.w d0,xxx.L takes up 3 words in memory and move.w d0,d16(An) takes up 2. Meanwhile, a simpler instruction like move.w d0,d0 only requires the word for the instruction itself and so takes up only 1 word in memory.

*) To be a bit more precise, it adds 4 cycles for each word fetched for the instruction, plus whatever amount of cycles is needed to get/store the required data in memory. So a move.w d0,d0 has no extension words and needs just 4 cycles (to read the single instruction word), while a move.w d0,(a0) also has no extension words but takes 8 cycles: 4 cycles to read the instruction word and 4 cycles to store d0 to the address pointed to by a0.

It can get a bit more complicated with other addressing modes and more complication instructions such as multiply/divide and shift. It also changes quite a bit with processors above the 68010. But '4 cycles per word accessed in memory' is the base rule for the 68000/010.

Last edited by roondar; 27 May 2021 at 10:14.
roondar is offline  
Old 27 May 2021, 14:25   #5
phx
Natteravn
 
phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,496
Quote:
Originally Posted by burma-shave View Post
So I'm guessing it was expected that _custom could vary between machines, but even then, it's not being resolved at runtime so I'm not sure what the above style is giving you.
I think that _custom was primarily defined for C compilers, as you need a symbol with a type to make references like
custom.intena
. And it hides the ugly numbers, so a
lea _custom,a0
is more readable than
lea $dff000,a0
.
phx is offline  
Old 27 May 2021, 15:58   #6
kamelito
Zone Friend
 
kamelito's Avatar
 
Join Date: May 2006
Location: France
Posts: 1,801
Using this method make you loose one address register though don’t know if that matters.
kamelito is offline  
Old 27 May 2021, 16:14   #7
CFou!
Moderator
 
CFou!'s Avatar
 
Join Date: Sep 2004
Location: France
Age: 50
Posts: 4,277
Quote:
Originally Posted by burma-shave View Post
I'm working my way through the Amiga Hardware Reference Manual). I'm new to programming at this level and am a bit perplexed by the style used to access hardware registers. The examples in the manual load the base address of the hardware registers into an address register and then use offsets to access each of the registers.

From Page 10 (3rd edition):
Code:
XREF      _custom


lea       _custom,a0
move.w    #$7FFF,intena(a0)
https://archive.org/details/commodor...age/8/mode/1up



So I'm guessing it was expected that _custom could vary between machines, but even then, it's not being resolved at runtime so I'm not sure what the above style is giving you.
you can replace by:
move.w #$7fff,$dff09a

to disable all interrupts

ps:
custom= $dff000 (not vary between machines)
intena=$9a
CFou! is offline  
Old 27 May 2021, 23:54   #8
Antiriad_UK
OCS forever!
 
Antiriad_UK's Avatar
 
Join Date: Mar 2019
Location: Birmingham, UK
Posts: 418
Quote:
Originally Posted by kamelito View Post
Using this method make you loose one address register though don’t know if that matters.
You'll find that most demos are coded with custom in a register all the time unless you run out. I use a5 for a "variables" and a6 for "custom" and most of my routines leave those registers alone unless absolutely required.
Antiriad_UK is offline  
Old 28 May 2021, 00:14   #9
roondar
Registered User
 
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,408
Personally, I almost always reserve A6 for "custom". I find that for me the code is just more readable when I use custom.i and add prefixes. For me, move.w #<blah>,dmacon(a6) is just nicer code than alternatives such as move.w #<blah>,$dff096. I understand others might have different opinions on this, but at the end of the day - this is why I use that style. The speed is a nice bonus.
roondar is offline  
Old 29 May 2021, 01:29   #10
burma-shave
Registered User
 
Join Date: Mar 2021
Location: Seattle / USA
Posts: 17
Thanks all for the great answers. Thanks @roondar for the detailed response. I think I'll be sticking with the readable offset style - great for me as a beginner.
burma-shave is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
byte write to custom registers / Bubble bobble "black screen" jotd Coders. Asm / Hardware 5 29 March 2020 16:50
log write to custom registers with out of bounds addresses jotd support.WinUAE 12 03 February 2019 13:43
FastRAM base address has no effect jotd support.WinUAE 6 14 July 2018 15:14
Checking custom registers in a game/demo Photon support.WinUAE 19 24 November 2009 16:03
auto loading save states for each custom config,how? kirk support.WinUAE 11 23 November 2006 18:56

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 16:53.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.08770 seconds with 15 queries