Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/psx/octoshock/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((unsigned long)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
//I think this is crap about file format (buffer size) change recovery. screw it.
33
//read_pos %= data.size();
34
//write_pos %= data.size();
35
//in_count %= (data.size() + 1);
36
}
37
38
#if 0
39
INLINE int StateAction(StateMem *sm, int load, int data_only, const char* sname)
40
{
41
SFORMAT StateRegs[] =
42
{
43
std::vector<T> data;
44
uint32 size;
45
46
SFVAR(read_pos),
47
SFVAR(write_pos),
48
SFVAR(in_count),
49
SFEND;
50
}
51
int ret = MDFNSS_StateAction(sm, load, data_only, sname);
52
53
if(load)
54
{
55
read_pos %= data.size();
56
write_pos %= data.size();
57
in_count %= (data.size() + 1);
58
}
59
60
return(ret);
61
}
62
#endif
63
64
INLINE uint32 CanRead(void)
65
{
66
return(in_count);
67
}
68
69
INLINE uint32 CanWrite(void)
70
{
71
return(size - in_count);
72
}
73
74
INLINE T ReadUnit(bool peek = false)
75
{
76
T ret;
77
78
assert(in_count > 0);
79
80
ret = data[read_pos];
81
82
if(!peek)
83
{
84
read_pos = (read_pos + 1) & (data.size() - 1);
85
in_count--;
86
}
87
88
return(ret);
89
}
90
91
INLINE uint8 ReadByte(bool peek = false)
92
{
93
assert(sizeof(T) == 1);
94
95
return(ReadUnit(peek));
96
}
97
98
INLINE void Write(const T *happy_data, uint32 happy_count)
99
{
100
assert(CanWrite() >= happy_count);
101
102
while(happy_count)
103
{
104
data[write_pos] = *happy_data;
105
106
write_pos = (write_pos + 1) & (data.size() - 1);
107
in_count++;
108
happy_data++;
109
happy_count--;
110
}
111
}
112
113
INLINE void WriteUnit(const T& wr_data)
114
{
115
Write(&wr_data, 1);
116
}
117
118
INLINE void WriteByte(const T& wr_data)
119
{
120
assert(sizeof(T) == 1);
121
Write(&wr_data, 1);
122
}
123
124
125
INLINE void Flush(void)
126
{
127
read_pos = 0;
128
write_pos = 0;
129
in_count = 0;
130
}
131
132
//private:
133
std::vector<T> data;
134
uint32 size;
135
uint32 read_pos; // Read position
136
uint32 write_pos; // Write position
137
uint32 in_count; // Number of units in the FIFO
138
139
template<bool isReader> void SyncState(EW::NewState *ns)
140
{
141
//I dont like this class...
142
143
PSS(&data[0], data.capacity()*sizeof(T));
144
NSS(read_pos);
145
NSS(write_pos);
146
NSS(in_count);
147
148
149
SaveStatePostLoad();
150
}
151
152
};
153
154
155
#endif
156
157