summaryrefslogtreecommitdiffstats
path: root/src/common/psw.c
blob: 36ab5e222bc60c2f2c705420f58ab969b8a522b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * psw.c
 *
 * Copyright (C) 2013 Hugo Villeneuve <hugo@hugovil.com>
 *
 * This file is released under the GPLv2
 */

#include "common.h"
#include "reg8051.h"
#include "memory.h"

/* Returns 0 or 1 */
int
psw_read_bit(int bit)
{
	return (mem_read8(INT_MEM_ID, _PSW_) >> bit) & 0x01;
}

void
psw_write_bit(int bit, int val)
{
	uint8_t psw = mem_read8(INT_MEM_ID, _PSW_);

	if (val)
		psw |= (1 << bit);  /* Set */
	else
		psw &= ~(1 << bit); /* Clear */

	mem_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_bit(PSW_BIT_CY, 1);
}

void
psw_clr_cy(void)
{
	psw_write_bit(PSW_BIT_CY, 0);
}

/* Returns 0 or 1 */
int
psw_read_ac(void)
{
	return psw_read_bit(PSW_BIT_AC);
}

void
psw_write_ac(int ac)
{
	psw_write_bit(PSW_BIT_AC, ac);
}

void
psw_set_ac(void)
{
	psw_write_bit(PSW_BIT_AC, 1);
}

void
psw_clr_ac(void)
{
	psw_write_bit(PSW_BIT_AC, 0);
}

/* Returns 0 or 1 */
int
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)
{
	int parity = 0;
	uint8_t acc = mem_read8(INT_MEM_ID, _ACC_);

	while (acc) {
		parity = !parity;
		acc = acc & (acc - 1);
	}

	psw_write_bit(PSW_BIT_P, parity);
}