X-Git-Url: http://gitweb.hugovil.com/?a=blobdiff_plain;f=src%2Fpsw.c;h=09352adafc56f9487723e988adc22c170dca2e62;hb=8d9b71ab3848729da1cf90a38a599be0b08cbb5c;hp=2210d85218982422b5986173351b7a666a831141;hpb=84075f7a1709b2d05858dc5f866fb4e4eb385ce0;p=emu8051.git diff --git a/src/psw.c b/src/psw.c index 2210d85..09352ad 100644 --- a/src/psw.c +++ b/src/psw.c @@ -22,42 +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 |= 0x80; /* Set */ + if (val) + psw |= (1 << bit); /* Set */ else - psw &= ~0x80; /* 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) { - u_int8_t psw = memory_read8(INT_MEM_ID, _PSW_); + psw_write_bit(PSW_BIT_CY, 1); +} - psw |= 0x80; +void +psw_clr_cy(void) +{ + psw_write_bit(PSW_BIT_CY, 0); +} - memory_write8(INT_MEM_ID, _PSW_, psw); /* Save updated value */ +/* Returns 0 or 1 */ +int +psw_read_ac(void) +{ + return psw_read_bit(PSW_BIT_AC); } void -psw_clr_cy(void) +psw_write_ac(int ac) { - u_int8_t psw = memory_read8(INT_MEM_ID, _PSW_); + psw_write_bit(PSW_BIT_AC, ac); +} - psw &= ~0x80; +void +psw_set_ac(void) +{ + psw_write_bit(PSW_BIT_AC, 1); +} - memory_write8(INT_MEM_ID, _PSW_, psw); /* Save updated value */ +void +psw_clr_ac(void) +{ + psw_write_bit(PSW_BIT_AC, 0); } /* Returns 0 or 1 */ int -psw_read_cy(void) +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) { - return memory_read8(INT_MEM_ID, _PSW_) >> 7; + 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); }