Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/psx/mednadisc/cdrom/SimpleFIFO.h
2 views
1
#ifndef __MDFN_SIMPLEFIFO_H
2
#define __MDFN_SIMPLEFIFO_H
3
4
#include <vector>
5
#include <assert.h>
6
7
#include "../math_ops.h"
8
9
template<typename T>
10
class SimpleFIFO
11
{
12
public:
13
14
// Constructor
15
SimpleFIFO(uint32 the_size) // Size should be a power of 2!
16
{
17
data.resize(round_up_pow2(the_size));
18
size = the_size;
19
read_pos = 0;
20
write_pos = 0;
21
in_count = 0;
22
}
23
24
// Destructor
25
INLINE ~SimpleFIFO()
26
{
27
28
}
29
30
INLINE void SaveStatePostLoad(void)
31
{
32
read_pos %= data.size();
33
write_pos %= data.size();
34
in_count %= (data.size() + 1);
35
}
36
37
#if 0
38
INLINE int StateAction(StateMem *sm, int load, int data_only, const char* sname)
39
{
40
SFORMAT StateRegs[] =
41
{
42
std::vector<T> data;
43
uint32 size;
44
45
SFVAR(read_pos),
46
SFVAR(write_pos),
47
SFVAR(in_count),
48
SFEND;
49
}
50
int ret = MDFNSS_StateAction(sm, load, data_only, sname);
51
52
if(load)
53
{
54
read_pos %= data.size();
55
write_pos %= data.size();
56
in_count %= (data.size() + 1);
57
}
58
59
return(ret);
60
}
61
#endif
62
63
INLINE uint32 CanRead(void)
64
{
65
return(in_count);
66
}
67
68
INLINE uint32 CanWrite(void)
69
{
70
return(size - in_count);
71
}
72
73
INLINE T ReadUnit(bool peek = false)
74
{
75
T ret;
76
77
assert(in_count > 0);
78
79
ret = data[read_pos];
80
81
if(!peek)
82
{
83
read_pos = (read_pos + 1) & (data.size() - 1);
84
in_count--;
85
}
86
87
return(ret);
88
}
89
90
INLINE uint8 ReadByte(bool peek = false)
91
{
92
assert(sizeof(T) == 1);
93
94
return(ReadUnit(peek));
95
}
96
97
INLINE void Write(const T *happy_data, uint32 happy_count)
98
{
99
assert(CanWrite() >= happy_count);
100
101
while(happy_count)
102
{
103
data[write_pos] = *happy_data;
104
105
write_pos = (write_pos + 1) & (data.size() - 1);
106
in_count++;
107
happy_data++;
108
happy_count--;
109
}
110
}
111
112
INLINE void WriteUnit(const T& wr_data)
113
{
114
Write(&wr_data, 1);
115
}
116
117
INLINE void WriteByte(const T& wr_data)
118
{
119
assert(sizeof(T) == 1);
120
Write(&wr_data, 1);
121
}
122
123
124
INLINE void Flush(void)
125
{
126
read_pos = 0;
127
write_pos = 0;
128
in_count = 0;
129
}
130
131
//private:
132
std::vector<T> data;
133
uint32 size;
134
uint32 read_pos; // Read position
135
uint32 write_pos; // Write position
136
uint32 in_count; // Number of units in the FIFO
137
};
138
139
140
#endif
141
142