Add parity bit update each instruction cycle
authorHugo Villeneuve <hugo@hugovil.com>
Mon, 18 Nov 2013 03:52:00 +0000 (22:52 -0500)
committerHugo Villeneuve <hugo@hugovil.com>
Mon, 18 Nov 2013 03:52:00 +0000 (22:52 -0500)
src/cpu8051.c
src/psw.c
src/psw.h

index 58a0e79..55e761a 100644 (file)
@@ -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();
index 4dd2a67..2d9c157 100644 (file)
--- 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);
+}
index f4c1bb4..1c87524 100644 (file)
--- 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 */