X-Git-Url: http://gitweb.hugovil.com/?a=blobdiff_plain;f=src%2Fpsw.c;h=09352adafc56f9487723e988adc22c170dca2e62;hb=8d9b71ab3848729da1cf90a38a599be0b08cbb5c;hp=d2b9117efde919c9db4de3c9155e8e1f6e3f2fbb;hpb=eff770859b67224a711339b0fa61033d977d03b6;p=emu8051.git diff --git a/src/psw.c b/src/psw.c index d2b9117..09352ad 100644 --- a/src/psw.c +++ b/src/psw.c @@ -22,34 +22,116 @@ #include "reg8051.h" #include "memory.h" +/* Returns 0 or 1 */ +int +psw_read_bit(int bit) +{ + return (memory_read8(INT_MEM_ID, _PSW_) >> bit) & 0x01; +} + void -psw_write_cy(int cy) +psw_write_bit(int bit, int val) { u_int8_t psw = memory_read8(INT_MEM_ID, _PSW_); - if (cy) - psw |= PSW_FLAG_CY; /* Set */ + if (val) + psw |= (1 << bit); /* Set */ else - psw &= ~PSW_FLAG_CY; /* Clear */ + psw &= ~(1 << bit); /* Clear */ memory_write8(INT_MEM_ID, _PSW_, psw); /* Save updated value */ } +/* Returns 0 or 1 */ +int +psw_read_cy(void) +{ + return psw_read_bit(PSW_BIT_CY); +} + +void +psw_write_cy(int cy) +{ + psw_write_bit(PSW_BIT_CY, cy); +} + void psw_set_cy(void) { - psw_write_cy(1); + psw_write_bit(PSW_BIT_CY, 1); } void psw_clr_cy(void) { - psw_write_cy(0); + psw_write_bit(PSW_BIT_CY, 0); } /* Returns 0 or 1 */ int -psw_read_cy(void) +psw_read_ac(void) +{ + return psw_read_bit(PSW_BIT_AC); +} + +void +psw_write_ac(int ac) { - return memory_read8(INT_MEM_ID, _PSW_) >> PSW_BIT_CY; + psw_write_bit(PSW_BIT_AC, ac); +} + +void +psw_set_ac(void) +{ + psw_write_bit(PSW_BIT_AC, 1); +} + +void +psw_clr_ac(void) +{ + psw_write_bit(PSW_BIT_AC, 0); +} + +/* Returns 0 or 1 */ +int +psw_read_ov(void) +{ + return psw_read_bit(PSW_BIT_OV); +} + +void +psw_write_ov(int ov) +{ + psw_write_bit(PSW_BIT_OV, ov); +} + +void +psw_set_ov(void) +{ + psw_write_bit(PSW_BIT_OV, 1); +} + +void +psw_clr_ov(void) +{ + psw_write_bit(PSW_BIT_OV, 0); +} + +/* + * Compute parity of bits in accumulator: + * parity = 0: even number of ones in accumulator + * parity = 1: odd number of ones in accumulator + */ +void +psw_compute_parity_bit(void) +{ + int parity = 0; + uint8_t acc = memory_read8(INT_MEM_ID, _ACC_); + + while (acc) { + parity = !parity; + acc = acc & (acc - 1); + } + + psw_write_bit(PSW_BIT_P, parity); }