-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfifo.cpp
More file actions
110 lines (93 loc) · 1.47 KB
/
Copy pathfifo.cpp
File metadata and controls
110 lines (93 loc) · 1.47 KB
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
/*
Skelton for retropc emulator
Author : Takeda.Toshiya
Date : 2014.12.19-
[ fifo buffer ]
*/
#include <stdlib.h>
#include <malloc.h>
#include "fifo.h"
#include "fileio.h"
FIFO::FIFO(int s)
{
size = s;
buf = (int*)malloc(size * sizeof(int));
cnt = rpt = wpt = 0;
}
void FIFO::release()
{
free(buf);
}
void FIFO::clear()
{
cnt = rpt = wpt = 0;
}
void FIFO::write(int val)
{
if(cnt < size) {
buf[wpt++] = val;
if(wpt >= size) {
wpt = 0;
}
cnt++;
}
}
int FIFO::read()
{
int val = 0;
if(cnt) {
val = buf[rpt++];
if(rpt >= size) {
rpt = 0;
}
cnt--;
}
return val;
}
int FIFO::read_not_remove(int pt)
{
if(pt >= 0 && pt < cnt) {
pt += rpt;
if(pt >= size) {
pt -= size;
}
return buf[pt];
}
return 0;
}
void FIFO::write_not_push(int pt, int d)
{
if(pt >= 0 && pt < cnt) {
pt += wpt;
if(pt >= size) {
pt -= size;
}
buf[pt] = d;
}
}
int FIFO::count()
{
return cnt;
}
bool FIFO::full()
{
return (cnt == size);
}
bool FIFO::empty()
{
return (cnt == 0);
}
#define STATE_VERSION 1
bool FIFO::process_state(void *f, bool loading)
{
FILEIO *state_fio = (FILEIO *)f;
if(!state_fio->StateCheckUint32(STATE_VERSION)) {
return false;
}
state_fio->StateValue(size);
state_fio->StateArray(buf, size * sizeof(int), 1);
state_fio->StateValue(cnt);
state_fio->StateValue(rpt);
state_fio->StateValue(wpt);
return true;
}