26 May 2021, 23:33 | #1 |
Registered User
Join Date: Mar 2021
Location: Seattle / USA
Posts: 17
|
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) 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. |
26 May 2021, 23:44 | #2 |
Registered User
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,410
|
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 |
27 May 2021, 00:25 | #3 |
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. |
27 May 2021, 09:41 | #4 |
Registered User
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,410
|
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. |
27 May 2021, 14:25 | #5 | |
Natteravn
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,496
|
Quote:
custom.intena. And it hides the ugly numbers, so a lea _custom,a0is more readable than lea $dff000,a0. |
|
27 May 2021, 15:58 | #6 |
Zone Friend
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.
|
27 May 2021, 16:14 | #7 | |
Moderator
Join Date: Sep 2004
Location: France
Age: 50
Posts: 4,277
|
Quote:
move.w #$7fff,$dff09a to disable all interrupts ps: custom= $dff000 (not vary between machines) intena=$9a |
|
27 May 2021, 23:54 | #8 |
OCS forever!
Join Date: Mar 2019
Location: Birmingham, UK
Posts: 418
|
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.
|
28 May 2021, 00:14 | #9 |
Registered User
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,410
|
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.
|
29 May 2021, 01:29 | #10 |
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.
|
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 |
|
|