From 67435359495e9090503b4d1003b468564de8a7b3 Mon Sep 17 00:00:00 2001 From: Hugo Villeneuve Date: Wed, 20 Nov 2013 23:27:37 -0500 Subject: [PATCH] Add PSW generic bit write/read functions --- src/psw.c | 88 +++++++++++++++++++++++-------------------------------- src/psw.h | 29 ++++++++++-------- 2 files changed, 53 insertions(+), 64 deletions(-) diff --git a/src/psw.c b/src/psw.c index 2d9c157..09352ad 100644 --- a/src/psw.c +++ b/src/psw.c @@ -22,113 +22,99 @@ #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 memory_read8(INT_MEM_ID, _PSW_) >> PSW_BIT_CY; + return psw_read_bit(PSW_BIT_AC); } void psw_write_ac(int ac) { - u_int8_t psw = memory_read8(INT_MEM_ID, _PSW_); - - if (ac) - psw |= PSW_FLAG_AC; /* Set */ - else - psw &= ~PSW_FLAG_AC; /* Clear */ - - memory_write8(INT_MEM_ID, _PSW_, psw); /* Save updated value */ + psw_write_bit(PSW_BIT_AC, ac); } void psw_set_ac(void) { - psw_write_ac(1); + psw_write_bit(PSW_BIT_AC, 1); } void psw_clr_ac(void) { - psw_write_ac(0); + psw_write_bit(PSW_BIT_AC, 0); } /* Returns 0 or 1 */ int -psw_read_ac(void) +psw_read_ov(void) { - return memory_read8(INT_MEM_ID, _PSW_) >> PSW_BIT_AC; + return psw_read_bit(PSW_BIT_OV); } void psw_write_ov(int ov) { - u_int8_t psw = memory_read8(INT_MEM_ID, _PSW_); - - if (ov) - psw |= PSW_FLAG_OV; /* Set */ - else - psw &= ~PSW_FLAG_OV; /* Clear */ - - memory_write8(INT_MEM_ID, _PSW_, psw); /* Save updated value */ + psw_write_bit(PSW_BIT_OV, ov); } void psw_set_ov(void) { - psw_write_ov(1); + psw_write_bit(PSW_BIT_OV, 1); } void psw_clr_ov(void) { - psw_write_ov(0); -} - -/* Returns 0 or 1 */ -int -psw_read_ov(void) -{ - return memory_read8(INT_MEM_ID, _PSW_) >> PSW_BIT_OV; -} - -void -psw_write_p(int p) -{ - u_int8_t psw = memory_read8(INT_MEM_ID, _PSW_); - - if (p) - psw |= PSW_FLAG_P; /* Set */ - else - psw &= ~PSW_FLAG_P; /* Clear */ - - memory_write8(INT_MEM_ID, _PSW_, psw); /* Save updated value */ + psw_write_bit(PSW_BIT_OV, 0); } /* @@ -147,5 +133,5 @@ psw_compute_parity_bit(void) acc = acc & (acc - 1); } - psw_write_p(parity); + psw_write_bit(PSW_BIT_P, parity); } diff --git a/src/psw.h b/src/psw.h index 1c87524..f2c09ba 100644 --- a/src/psw.h +++ b/src/psw.h @@ -23,44 +23,47 @@ #include -void -psw_set_cy(void); - -void -psw_clr_cy(void); +int +psw_read_bit(int bit); void -psw_write_cy(int cy); +psw_write_bit(int bit, int val); int psw_read_cy(void); void -psw_set_ac(void); +psw_write_cy(int cy); void -psw_clr_ac(void); +psw_clr_cy(void); void -psw_write_ac(int ac); +psw_set_cy(void); int psw_read_ac(void); void -psw_set_ov(void); +psw_write_ac(int ac); void -psw_clr_ov(void); +psw_clr_ac(void); void -psw_write_ov(int ov); +psw_set_ac(void); int psw_read_ov(void); void -psw_write_p(int p); +psw_write_ov(int ov); + +void +psw_clr_ov(void); + +void +psw_set_ov(void); void psw_compute_parity_bit(void); -- 2.20.1