Map range: Difference between revisions

No edit summary
Line 46:
</pre>
 
=={{header|6502 Assembly}}==
A range like [0, n] for some natural number <code>n < 255</code> can be easily mapped with a lookup table. The second range can be anything, as long as each entry is the same length and are stored consecutively in the desired order. This method requires a bit of compile-time knowledge, but is very efficient in terms of speed. Each element <code>Bn</code> is stored at relative offset <code>An</code> .
 
<lang 6502asm>mapping:
byte $00,$00,$80,$BF ;-1.0f
byte $66,$66,$66,$BF ;-0.9f
byte $CD,$CC,$4C,$BF ;-0.8f
byte $33,$33,$33,$BF ;-0.7f
etc.</lang>
 
If the range isn't [0, n], but begins at some other natural number [k, n] where <tt>k,n < 255 </tt>, we can express it as [0, n-k] instead and simply subtract the "key" at runtime to get the offset.
 
This method is very convenient for implementing ASCII into hardware that lacks a built-in system font (like the NES). You can save graphics memory by mapping tile number 0 to ASCII 32, tile number 01 to 33, and so on. Had you mapped them "correctly," (i.e. tile number 32 to ASCII 32) you would still need a "blank tile" as tile number zero. So the easier solution is to subtract 32 from the character code before printing.
 
<lang 6502asm>
PrintChar: ;runs during non-maskable interrupt.
;a = char to print
SEC
SBC #$32 ;subtract ascii offset to map the index to the correct tile graphics data.
 
;everything below this comment is hardware-specific mumbo-jumbo, feel free to ignore it if you don't care.
;ideally you'd want to do this before getting here so that the only thing that happens during NMI is the write to vram.
pha
LDA Cursor_Y
ASL
ASL
ASL
ASL
ASL
STA tempY ;row * 32
LDA #$20
ADC Cursor_X
STA tempX
LDA $2002 ;reset picture processor high-low latch
LDA tempX
STA $2006 ;this register is big-endian for some reason. Which is why I had to store Cursor_Y << 5 into tempY rather than here directly.
LDA tempY
STA $2006
PLA
STA $2007</lang>
=={{header|ACL2}}==
 
1,489

edits