Mother2GbaTranslation/m2-formatting.asm

273 lines
4.5 KiB
NASM
Raw Normal View History

2015-03-13 23:09:31 +00:00
//==============================================================================
// http://en.wikipedia.org/wiki/Double_dabble
// int bin_to_bcd(int binary)
// In:
// r0: binary number
// Out:
// r0: BCD-coded number
// r1: amount of digits used
//==============================================================================
2017-03-29 05:19:51 +01:00
bin_to_bcd:
2015-03-13 23:09:31 +00:00
push {r2-r7,lr}
mov r2,r8
mov r3,r9
push {r2-r3}
2017-03-29 05:19:51 +01:00
lsl r0,r0,5
mov r1,0
mov r3,27
mov r4,3
lsl r4,r4,8
lsl r5,r4,4
lsl r6,r5,4
lsl r7,r6,12
2015-03-13 23:09:31 +00:00
mov r9,r7
2017-03-29 05:19:51 +01:00
lsl r7,r6,8
2015-03-13 23:09:31 +00:00
mov r8,r7
2017-03-29 05:19:51 +01:00
lsl r7,r6,4
2015-03-13 23:09:31 +00:00
//--------------------------------
2017-03-29 05:19:51 +01:00
@@prev:
2015-03-13 23:09:31 +00:00
// Ones
2017-03-29 05:19:51 +01:00
lsl r2,r1,28
lsr r2,r2,28
cmp r2,5
bcc @@next
add r1,r1,3
@@next:
2015-03-13 23:09:31 +00:00
// Tens
2017-03-29 05:19:51 +01:00
lsl r2,r1,24
lsr r2,r2,28
cmp r2,5
bcc @@next2
add r1,0x30
@@next2:
2015-03-13 23:09:31 +00:00
// Hundreds
2017-03-29 05:19:51 +01:00
lsl r2,r1,20
lsr r2,r2,28
cmp r2,5
bcc @@next3
2015-03-13 23:09:31 +00:00
add r1,r1,r4
2017-03-29 05:19:51 +01:00
@@next3:
2015-03-13 23:09:31 +00:00
// Thousands
2017-03-29 05:19:51 +01:00
lsl r2,r1,16
lsr r2,r2,28
cmp r2,5
bcc @@next4
2015-03-13 23:09:31 +00:00
add r1,r1,r5
2017-03-29 05:19:51 +01:00
@@next4:
2015-03-13 23:09:31 +00:00
// Ten-thousands
2017-03-29 05:19:51 +01:00
lsl r2,r1,12
lsr r2,r2,28
cmp r2,5
bcc @@next5
2015-03-13 23:09:31 +00:00
add r1,r1,r6
2017-03-29 05:19:51 +01:00
@@next5:
2015-03-13 23:09:31 +00:00
// Hundred-thousands
2017-03-29 05:19:51 +01:00
lsl r2,r1,8
lsr r2,r2,28
cmp r2,5
bcc @@next6
2015-03-13 23:09:31 +00:00
add r1,r1,r7
2017-03-29 05:19:51 +01:00
@@next6:
2015-03-13 23:09:31 +00:00
// Millions
2017-03-29 05:19:51 +01:00
lsl r2,r1,4
lsr r2,r2,28
cmp r2,5
bcc @@next7
2015-03-13 23:09:31 +00:00
add r1,r8
2017-03-29 05:19:51 +01:00
@@next7:
2015-03-13 23:09:31 +00:00
// Ten-millions
2017-03-29 05:19:51 +01:00
lsr r2,r2,28
cmp r2,5
bcc @@next8
2015-03-13 23:09:31 +00:00
add r1,r9
2017-03-29 05:19:51 +01:00
@@next8:
2015-03-13 23:09:31 +00:00
// Shift
2017-03-29 05:19:51 +01:00
lsl r1,r1,1
2015-03-13 23:09:31 +00:00
tst r0,r0
2017-03-29 05:19:51 +01:00
bpl @@next9
add r1,r1,1
@@next9:
lsl r0,r0,1
sub r3,r3,1
bne @@prev
2015-03-13 23:09:31 +00:00
mov r0,r1
//--------------------------------
// Check how many digits are used
2017-03-29 05:19:51 +01:00
mov r3,1
@@prev2:
lsr r1,r1,4
beq @@next10
add r3,r3,1
b @@prev2
@@next10:
2015-03-13 23:09:31 +00:00
//--------------------------------
mov r1,r3
pop {r2-r3}
mov r8,r2
mov r9,r3
pop {r2-r7,pc}
//==============================================================================
// int bcd_to_bin(int bcd)
// In:
// r0: BCD-coded number
// Out:
// r0: binary number
//==============================================================================
2017-03-29 05:19:51 +01:00
bcd_to_bin:
2015-03-13 23:09:31 +00:00
push {r1-r4,lr}
2017-03-29 05:19:51 +01:00
mov r1,0
mov r2,10
mov r3,1
2015-03-13 23:09:31 +00:00
//--------------------------------
// Ones
2017-03-29 05:19:51 +01:00
lsl r4,r0,28
lsr r4,r4,28
2015-03-13 23:09:31 +00:00
mul r4,r3
add r1,r1,r4
mul r3,r2
// Tens
2017-03-29 05:19:51 +01:00
lsl r4,r0,24
lsr r4,r4,28
2015-03-13 23:09:31 +00:00
mul r4,r3
add r1,r1,r4
mul r3,r2
// Hundreds
2017-03-29 05:19:51 +01:00
lsl r4,r0,20
lsr r4,r4,28
2015-03-13 23:09:31 +00:00
mul r4,r3
add r1,r1,r4
mul r3,r2
// Thousands
2017-03-29 05:19:51 +01:00
lsl r4,r0,16
lsr r4,r4,28
2015-03-13 23:09:31 +00:00
mul r4,r3
add r1,r1,r4
mul r3,r2
// Ten-thousands
2017-03-29 05:19:51 +01:00
lsl r4,r0,12
lsr r4,r4,28
2015-03-13 23:09:31 +00:00
mul r4,r3
add r1,r1,r4
mul r3,r2
// Hundred-thousands
2017-03-29 05:19:51 +01:00
lsl r4,r0,8
lsr r4,r4,28
2015-03-13 23:09:31 +00:00
mul r4,r3
add r1,r1,r4
mul r3,r2
// Millions
2017-03-29 05:19:51 +01:00
lsl r4,r0,4
lsr r4,r4,28
2015-03-13 23:09:31 +00:00
mul r4,r3
add r1,r1,r4
mul r3,r2
// Ten-millions
2017-03-29 05:19:51 +01:00
lsr r4,r0,28
2015-03-13 23:09:31 +00:00
mul r4,r3
add r1,r1,r4
//--------------------------------
mov r0,r1
pop {r1-r4,pc}
//==============================================================================
// ushort format_cash(int amount, char* output, int digits)
// In:
// r0: amount
// r1: output string
// r2: digits
// Out:
// r0: = digits - (digits rendered) - 1
//==============================================================================
2017-03-29 05:19:51 +01:00
format_cash:
2015-03-13 23:09:31 +00:00
push {r1-r7,lr}
mov r4,r1
mov r6,r2
//--------------------------------
// Figure out how many digits we need
2017-03-29 05:19:51 +01:00
bl bin_to_bcd
2015-03-13 23:09:31 +00:00
mov r5,r1
mov r7,r1
// The window is 56 pixels wide:
2017-03-29 05:19:51 +01:00
// each digit uses 6 pixels, the 0x sign uses 6, and the
2015-03-13 23:09:31 +00:00
// double-zero uses 8
2017-03-29 05:19:51 +01:00
mov r2,42 // = 56 - 6 - 8
lsl r3,r1,2
2015-03-13 23:09:31 +00:00
add r3,r3,r1
add r1,r1,r3 // r1 *= 6
sub r1,r2,r1 // r1 = 42 - r1
// Store the control code to the output
2017-03-29 05:19:51 +01:00
mov r2,0x5F
strb r2,[r4,0]
mov r2,0xFF
strb r2,[r4,1]
strb r1,[r4,2]
2015-03-13 23:09:31 +00:00
// Store the dollar sign
2017-03-29 05:19:51 +01:00
mov r2,0x54
strb r2,[r4,3]
add r4,r4,4
2015-03-13 23:09:31 +00:00
// Store the digits to the output
2017-03-29 05:19:51 +01:00
mov r2,8
2015-03-13 23:09:31 +00:00
sub r2,r2,r5
2017-03-29 05:19:51 +01:00
lsl r2,r2,2
2015-03-13 23:09:31 +00:00
lsl r0,r2 // Now the number is pushed to the left of the register
2017-03-29 05:19:51 +01:00
@@prev:
lsr r1,r0,28 // Get the left-most digit
add r1,0x60 // Integer-to-char
strb r1,[r4,0]
add r4,r4,1
lsl r0,r0,4
sub r5,r5,1
bne @@prev
2015-03-13 23:09:31 +00:00
// Store the double-zero sign
2017-03-29 05:19:51 +01:00
mov r1,0x56
strb r1,[r4,0]
2015-03-13 23:09:31 +00:00
// Store the end code
2017-03-29 05:19:51 +01:00
mov r1,0
strb r1,[r4,1]
mov r1,0xFF
strb r1,[r4,2]
2015-03-13 23:09:31 +00:00
//--------------------------------
sub r5,r6,r7
2017-03-29 05:19:51 +01:00
sub r0,r5,1
2015-03-13 23:09:31 +00:00
pop {r1-r7,pc}