lmichaelis/Computer-Simulation

Sub doesn't seem to resolve correctly for results < zero

Closed this issue · 7 comments

I have some other suggestions I was thinking of looking at too:

Add decimal output

class _OUTInstruction(_Instruction):
    def process(self, computer):
        print('[OUT] %s - %03d' % (computer._a, int(computer._a, 2))) # include decimal in output
        computer._pc += 1

    def get_basename(self) -> str:
        return 'OUT'

Looks like negatives are not handled correctly. But maybe I am still having to learn more about binary.
This program should load 10 into A, subtract 20 from A and output -10 (11110110 in twos compliment)

➜  Computer-Simulation git:(temp) ✗ cat negative.prg
LDA 15
SUB 14
OUT 0
HLT 0

14: 20
15: 10

but when I compile and run this, I get:

➜  Computer-Simulation git:(temp) ✗ ./cpu.py negative.prg.bin
Loglevel: INFO
[OUT] 11111111 - 255

It works perfectly in cases where the result of the SUB call is > 0.

I will see if there is anything I can do to sort that out. It will mean learning how you structured your code a bit more. But it might be fun...

@linuxplayground It seems this is intended behavior. On line 65 of instr.py, we have x = '11111111' which is causing the output you noted. Comment out this line and the program provides the correct output, albeit with a '-' sign instead of two's complement: -1010. I'm assuming that this is because the author intends by default for everything to be handled as unsigned binary, so setting to 255 is meant to indicate possible overflow.

yeah - I saw that line in the code when I was digging after raising this issue. (issue isn't the right word. I just didn't know how to raise it as a topic for discussion.)

I started looking at implementing twos compliment handling and got some python functions working that do that. However, while I was following that particular rabbit hole and catching exceptions etc, I realized that exception handling at this particular level would move the emulator from something that closely emulates Ben's CPU to something that does not. IE: Ben's CPU doesn't have exception handling It just overflows the bits and sets the carry bit.

So there probably isn't a good solution to this. I was only hoping to emulate the 7 segment display correctly. IE: Show the negative sign for signed values as they appear on the bus.

Is the negative sign not showing for you? This is the output I get from your program after commenting out line 65:
image

I suppose this isn't an exact simulation since the sign is always left-aligned on the 8bc but it is close.

doh- forgot to comment out the line as you suggested and it does indeed work.

I will resolve this "issue". Thanks for helping.

No problem! Glad I could help

@lmichaelis in Ben's design there's a jumper to switch from unsigned (0-255) to signed (-128-127). Is there a way to accomplish that switch with your code that I'm missing?

@lmichaelis in Ben's design there's a jumper to switch from unsigned (0-255) to signed (-128-127). Is there a way to accomplish that switch with your code that I'm missing?

@n-tropy247 No, there is currently no way of working with signed values. Any value that would be signed is just set to 255 and the carry flag is set. While I currently don't intend to fix it, you'd certainly be welcome to try!