How to check for flags being set for 8085 simulator

Total Anime Immersion :
    byte x = 0x0c;
    byte y = 0x05;

    System.out.println((x&0xf) + (y&0xf)); // if > 15 then af on
    System.out.println((b&0xf) - (c&0xf));  
    System.out.println((int)(b&0xff) + (c&0xff)); // if > 255 then cf on    
    System.out.println(((int)(b&0xff) + (c&0xff)) & 0b10000000); // if 128 sign flag set
    System.out.println((int)(b&0xff) - (c&0xff)); // if less than 0 then cf set
    System.out.println(((int)(b&0xff) - (c&0xff)) & 0b10000000); //if 128 then sign flag set

I am building an 8085 simulator, and under the Arithmetic & Logical Instructions, I was trying to find a reliable method to check when to set or unset the 5 flags.

Out of the 5, sign flag, zero flag and parity flag are easy to check for, however carry and auxiliary carry flag gets complex. My method of approaching to check the carry flag & auxiliary carry flag are posted above which don't seem very reliable.

Any better/cleaner/more optimal solution is welcome. I have also been using www.sim8085.com to check my mini code snippets and check which flags are getting turned on, here is where I arrived at my next question :

    mvi a, ffh
    mvi b, cch
    sub b
    hlt

When I run this code, the auxiliary flag gets set. From what I know, the auxiliary flag only gets set if there is a carry from the lower nibble to the higher nibble. Hence I'm not being able to understand the logic behind the same.

Would also like to know if all flags are only affected by changes to accumulator or otherwise as well?

EDIT:

        byte b = 0x2f;
        byte c = 0x2c;

        byte d = (byte) ((byte) (b&0xf) + (c&0xf)); 
        byte e = (byte) ((byte) (b&0xf) + ((~c)&0xf) + 1); 

        int x = (int) ((b&0xff) + (c&0xff));
        int y = (int) ((int) (b&0xff) + ((~c)&0xff) + 1); 

        System.out.println(y);
        System.out.println((d>0xf) + "\t" + (e>0xf));
        System.out.println((x>0xff) + "\t" + (y>0xff));

I tried using this logic to see if it works for all mathematical operations, apparently this doesn't make it as well to reliably check if carry or auxiliary carry flag should be set. Kindly help me figure out how to reliably check if an operation results in carry flag and auxiliary carry flag being set or unset.

Total Anime Immersion :
        byte b = (byte) 0x1c;
        byte c = (byte) 0xff;

        char a = (char) (Byte.toUnsignedInt(b) + Byte.toUnsignedInt(c));
        char d = (char) (Byte.toUnsignedInt(b) - Byte.toUnsignedInt(c));

        System.out.println(Integer.toHexString(a));
        System.out.println(Integer.toHexString(d));

        if(a > 0xff) {
            System.out.println("Add Carry");
        }

        if(d > 0xff) {
            System.out.println("Sub Carry");
        }

        if((Byte.toUnsignedInt(b) & 0xf) + (Byte.toUnsignedInt(c) & 0xf) > 0xf) {
            System.out.println("Add Aux Carry");
        }

        if((Byte.toUnsignedInt(b) & 0xf) + (Byte.toUnsignedInt((byte) ~c) & 0xf) + 1 > 0xf) {
            System.out.println("Sub Aux Carry");
        }

I did some further checking and figured char data type is unsigned in java and hence it can be used with the logic above for the required results.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=109234&siteId=1