unbibium: (animated pacman)
[personal profile] unbibium

import math

def cbm2float(e, m):
    if m==0:
        return 0
    m2 = (1 + ((m & 0x7FFFFFFF) / 0x80000000))
    e2 = (2 ** (e-129))
    print (m2, e2)
    if m & 0x80000000:
        return -(m2 * e2)
    else:
        return m2 * e2

def float2cbm(f):
    if f == 0: return (0, 0)
    e = int(math.log(f, 2))
    e2 = e + 129
    m2 = int(((f / (2 ** e))-1)  * 0x80000000) 
    if f < 0:
        return (e2, m2 + 0x80000000)
    else:
        return (e2, m2)

print(cbm2float(0x84, 0x20000000))
print("%x %x" % float2cbm(10))

print("PI in the C64 rom is at AEA8: $82,$49,$0F,$DA,$A1")
print("PI found by this program is: %x %x" % float2cbm(math.pi))

Date: 2013-01-15 02:11 am (UTC)
From: [identity profile] mmcirvin.livejournal.com
A five-byte floating point format. That's pretty unusual.

Date: 2013-01-15 07:43 am (UTC)
From: [identity profile] pentomino.livejournal.com
apparently there's an extra overflow byte in working memory during some intermediate operations, for extra accuracy. but variables are stored as 5 bytes.

i'm a few days from doing anything of substance with that, though. I'm still trying to get the startup text to display.

Date: 2013-01-15 01:38 pm (UTC)
From: [identity profile] mmcirvin.livejournal.com
I suppose that since the system is 8-bit, there's no great advantage to making the float some power-of-2 number of bytes.

Date: 2013-01-16 04:59 pm (UTC)
From: [identity profile] pentomino.livejournal.com
My DCPU-16 adaptation will make it a three-byte format (where bytes are 16 bits). One byte for the exponent, two for the mantissa. I'll leave the exponent as 8 bits, since increasing the precision of the exponent isn't really worth that much.

I also figured out how to easily convert an integer to this format:

* set the exponent byte to 0x81 plus the number of bits in your integer
* shift the integer left and subtract 1 from the exponent byte
* repeat the previous step until the first 1 bit falls off the left side

Date: 2013-01-15 04:01 pm (UTC)
From: [identity profile] mmcirvin.livejournal.com
The other main difference from the IEEE formats is that the sign bit is attached to the mantissa rather than leading the exponent.

Date: 2013-01-20 08:56 am (UTC)
From: [identity profile] pentomino.livejournal.com
When they're stored in program memory, the sign bit is attached to the mantissa. When they're being worked on, the sign bit is separated out, and the high bit of the mantissa is set to 1.

Profile

unbibium: (Default)
unbibium

June 2025

S M T W T F S
1234567
891011121314
151617 18192021
22232425262728
2930     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 12th, 2025 02:49 am
Powered by Dreamwidth Studios