diff --git a/conversion/i16tof32.z80 b/conversion/i16tof32.z80 new file mode 100644 index 0000000..4535a67 --- /dev/null +++ b/conversion/i16tof32.z80 @@ -0,0 +1,55 @@ +#ifndef included_i16tof32 +#define included_i16tof32 + +i16tof32: +;Inputs: +; HL holds a 16-bit signed integer, (-32768 to 32767) +; BC points to where to write the float +;Outputs: +; Converts A to an f32 float at BC +; + call pushpop + + xor a + ld (bc),a + inc bc + + ld a,l + or h + ld d,a + jr z,i16tof32_finish + + ld a,h + or a + jp p,$+10 + xor a + sub l + ld l,a + sbc a,a + sub h + ld h,a + scf + + rla ; save the sign + ld d,$7F+16 ;Initial exponent + + dec d + add hl,hl + jr nc,$-2 + + rra ; shift out the sign + rr d ; shift the exponent down, shifting in the sign + rr h ; shift the lsb of the exponent into the significand + rr l + + ld a,l +i16tof32_finish: + ld (bc),a + inc bc + ld a,h + ld (bc),a + inc bc + ld a,d + ld (bc),a + ret +#endif diff --git a/conversion/i8tof32.z80 b/conversion/i8tof32.z80 new file mode 100644 index 0000000..9838412 --- /dev/null +++ b/conversion/i8tof32.z80 @@ -0,0 +1,64 @@ +#ifndef included_i8tof32 +#define included_i8tof32 + +i8tof32: +;Inputs: +; A holds a 8-bit signed integer, (-128 to 127) +; BC points to where to write the float +;Outputs: +; Converts A to an f32 float at BC +; + push hl + push af + + or a + jp p,$+6 + neg + scf + +; #ifndef included_u8tof32 +; ##define included_u8tof32 +; .db $21 ; start of `ld hl,*` to skip the next byte +; u8tof32: +; push hl +; push af +; #else +; .echo "Hint: it looks like you are using u8tof32 as well as i8tof32. i8tof32 already has code for u8tof32, so you can save bytes by including it first." +; #endif +; + ; Begin writing the float + ld h,b + ld l,c + ld (hl),0 + inc hl + ld (hl),0 + inc hl + + jr nz,$+8 + ld (hl),a + inc hl + ld (hl),a + pop af + pop hl + ret + + push bc + rl c ; save the sign + ld b,$7F+8 ;Initial exponent + + dec b + add a,a + jr nc,$-2 + + rr c ; shift in a 1 and shift out the sign + rr b ; shift the exponent down, shifting in the sign + rra ; shift the lsb of the exponent into the significand + ld (hl),a + inc hl + ld (hl),b + + pop bc + pop af + pop hl + ret +#endif diff --git a/conversion/u16tof32.z80 b/conversion/u16tof32.z80 new file mode 100644 index 0000000..9c89186 --- /dev/null +++ b/conversion/u16tof32.z80 @@ -0,0 +1,42 @@ +#ifndef included_u16tof32 +#define included_u16tof32 + +u16tof32: +;Inputs: +; HL holds a 16-bit unsigned integer, (0 to 65535) +; BC points to where to write the float +;Outputs: +; Converts HL (unsigned) to an f32 float at BC +; + call pushpop + + xor a + ld (bc),a + inc bc + + ld a,l + or h + ld d,a + jr z,u16tof32_finish + + ld d,$7F+16 ;Initial exponent + + dec d + add hl,hl + jr nc,$-2 + + srl d ; shift the exponent down, shifting in the sign + rr h ; shift the lsb of the exponent into the significand + rr l + + ld a,l +u16tof32_finish: + ld (bc),a + inc bc + ld a,h + ld (bc),a + inc bc + ld a,d + ld (bc),a + ret +#endif diff --git a/conversion/u8tof32.z80 b/conversion/u8tof32.z80 new file mode 100644 index 0000000..839d5ff --- /dev/null +++ b/conversion/u8tof32.z80 @@ -0,0 +1,50 @@ +#ifndef included_u8tof32 +#define included_u8tof32 + +u8tof32: +;Inputs: +; A holds a 8-bit unsigned integer, (0 to 255) +; BC points to where to write the float +;Outputs: +; Converts A to an f32 float at BC +; + push hl + push af + + ; Begin writing the float + ld h,b + ld l,c + ld (hl),0 + inc hl + ld (hl),0 + inc hl + + or a + jr nz,$+8 + ld (hl),a + inc hl + ld (hl),a + pop af + pop hl + ret + + push bc + rl c ; save the sign + ld b,$7F+8 ;Initial exponent + + dec b + add a,a + jr nc,$-2 + + rr c ; shift in a 1 and shift out the sign + rr b ; shift the exponent down, shifting in the sign + rra ; shift the lsb of the exponent into the significand + ld (hl),a + inc hl + ld (hl),b + + pop bc + pop af + pop hl + ret +#endif