From: Hugo Villeneuve Date: Mon, 18 Nov 2013 03:52:00 +0000 (-0500) Subject: Add parity bit update each instruction cycle X-Git-Tag: v2.0.0~79 X-Git-Url: http://gitweb.hugovil.com/?a=commitdiff_plain;h=98b757f83d9e4be02e9373f3279d432d3f7113f5;p=emu8051.git Add parity bit update each instruction cycle --- 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 */