From 98b757f83d9e4be02e9373f3279d432d3f7113f5 Mon Sep 17 00:00:00 2001 From: Hugo Villeneuve Date: Sun, 17 Nov 2013 22:52:00 -0500 Subject: [PATCH] Add parity bit update each instruction cycle --- src/cpu8051.c | 7 +++++++ src/psw.c | 32 ++++++++++++++++++++++++++++++++ src/psw.h | 6 ++++++ 3 files changed, 45 insertions(+) diff --git a/src/cpu8051.c b/src/cpu8051.c index 58a0e79..55e761a 100644 --- a/src/cpu8051.c +++ b/src/cpu8051.c @@ -28,6 +28,7 @@ #include "reg8051.h" #include "cpu8051.h" #include "memory.h" +#include "psw.h" #include "disasm.h" #include "instructions_8051.h" @@ -374,6 +375,12 @@ cpu8051_Exec(void) cpu8051.pc++; insttiming = (*opcode_table[opcode])(); /* Function callback. */ + /* + * Parity bit (p): is automatically set or cleared in each machine + * cycle to establish even parity in the accumulator. + */ + psw_compute_parity_bit(); + for (i = 0; i < insttiming; i++) { cpu8051_CheckInterrupts(); cpu8051_DoTimers(); diff --git a/src/psw.c b/src/psw.c index 4dd2a67..2d9c157 100644 --- a/src/psw.c +++ b/src/psw.c @@ -117,3 +117,35 @@ 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 */ +} + +/* + * 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_p(parity); +} diff --git a/src/psw.h b/src/psw.h index f4c1bb4..1c87524 100644 --- a/src/psw.h +++ b/src/psw.h @@ -59,4 +59,10 @@ psw_write_ov(int ov); int psw_read_ov(void); +void +psw_write_p(int p); + +void +psw_compute_parity_bit(void); + #endif /* PSW_H */ -- 2.20.1