MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
P3Atom.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  P3Atom.h Atom with built in error correcting
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
27 #ifndef P3ATOM_H
28 #define P3ATOM_H
29 
30 #include <stdio.h>
31 #include "itype.h"
32 #include "Point.h"
33 #include "BitField.h"
34 #include "Atom.h"
35 #include "Element.h"
36 #include "CoreConfig.h"
37 #include "ParamConfig.h"
38 #include "Util.h" /* For COMPILATION_REQUIREMENT */
39 #include "Parity2D_4x4.h"
40 
41 namespace MFM {
42 
43  template <class PC>
44  class P3Atom : public Atom< CoreConfig< P3Atom<PC>, PC> >
45  {
46  public: enum
47  {
48  ATOM_CATEGORY = 3
49  };
50  private:
51  typedef CoreConfig< P3Atom<PC>, PC> CC;
52 
53  public:
54  enum {
55  BITS = PC::BITS_PER_ATOM,
56 
58  // P3 header configuration: Header is in low-bits end of the bitvector
59 
60  P3_ECC_BITS_POS = 0,
61  P3_ECC_BITS_LEN = 9,
62 
63  P3_TYPE_BITS_POS = P3_ECC_BITS_POS + P3_ECC_BITS_LEN,
64  P3_TYPE_BITS_LEN = 16,
65 
66  P3_FIXED_HEADER_POS = P3_ECC_BITS_POS,
67  P3_FIXED_HEADER_LEN = P3_ECC_BITS_LEN + P3_TYPE_BITS_LEN,
68 
69  P3_STATE_BITS_POS = P3_FIXED_HEADER_POS + P3_FIXED_HEADER_LEN,
70  P3_STATE_BITS_LEN = BITS - P3_STATE_BITS_POS,
71 
73  // Declarations required by the Atom contract
74  ATOM_FIRST_STATE_BIT = P3_STATE_BITS_POS,
75 
77  // Other constants
78  P3_TYPE_COUNT = 1<<P3_TYPE_BITS_LEN
79 
80  };
81 
82  typedef BitField<BitVector<BITS>,VD::U32,P3_FIXED_HEADER_LEN,P3_FIXED_HEADER_POS> AFFixedHeader;
83  typedef BitField<BitVector<BITS>,VD::U32,P3_TYPE_BITS_LEN,P3_TYPE_BITS_POS> AFTypeBits;
84  typedef BitField<BitVector<BITS>,VD::U32,P3_ECC_BITS_LEN,P3_ECC_BITS_POS> AFECCBits;
85 
86  protected:
87 
88  /* We really don't want to allow the public to change the type of a
89  P3Atom, since the type doesn't mean much without the atomic
90  header as well */
91 
92  void SetType(u32 type) {
93 
94  if (type >= P3_TYPE_COUNT)
95  FAIL(ILLEGAL_ARGUMENT);
96 
97  // Generate ECC and store all in header
99  }
100 
101  public:
102 
103  // P3Atom(u32 type = Element_Empty<CC>::THE_INSTANCE.GetType(), u32 z1 = 0, u32 z2 = 0, u32 stateBits = 0)
104  P3Atom(u32 type = 0, u32 z1 = 0, u32 z2 = 0, u32 stateBits = 0)
105  {
106  COMPILATION_REQUIREMENT< 32 <= BITS-1 >();
107 
108  if (z1 != 0 || z2 != 0)
109  FAIL(ILLEGAL_ARGUMENT);
110 
111  if (stateBits > P3_STATE_BITS_LEN)
112  FAIL(OUT_OF_ROOM);
113 
114  SetType(type);
115  }
116 
117  u32 GetTypeImpl() const {
118  return AFTypeBits::Read(this->m_bits);
119  }
120 
121  bool IsSaneImpl() const
122  {
123  u32 fixedHeader = AFFixedHeader::Read(this->m_bits);
124  return Parity2D_4x4::Check2DParity(fixedHeader);
125  }
126 
127  bool HasBeenRepairedImpl()
128  {
129  u32 fixedHeader = AFFixedHeader::Read(this->m_bits);
130  u32 repairedHeader =
132 
133  if (repairedHeader == 0) return false;
134 
135  if (fixedHeader != repairedHeader)
136  {
137  AFFixedHeader::Write(this->m_bits, repairedHeader);
138  }
139 
140  return true;
141  }
142 
143 
144  u32 GetMaxStateSize(u32 type) const {
145  return P3_STATE_BITS_LEN;
146  }
147 
151  u32 EndStateBit() const
152  {
153  return BITS;
154  }
155 
156  void WriteStateBitsImpl(ByteSink& ostream) const
157  {
158  for(u32 i = P3_STATE_BITS_POS; i < P3_STATE_BITS_POS + P3_STATE_BITS_LEN; i++)
159  {
160  ostream.Printf("%d", this->m_bits.ReadBit(i) ? 1 : 0);
161  }
162  }
163 
164  void ReadStateBitsImpl(const char* stateStr)
165  {
166  for(u32 i = 0; i < P3_STATE_BITS_LEN; i++)
167  {
168  this->m_bits.WriteBit(P3_STATE_BITS_POS + i, stateStr[i] == '0' ? 0 : 1);
169  }
170  }
171 
172  void ReadStateBitsImpl(const BitVector<BITS> & bv)
173  {
174  for(u32 i = 0; i < P3_STATE_BITS_LEN; i++)
175  {
176  u32 idx = P3_STATE_BITS_POS + i;
177  this->m_bits.WriteBit(idx, bv.ReadBit(idx));
178  }
179  }
180 
185  u32 GetStateField(u32 stateIndex, u32 stateWidth) const
186  {
187  if (stateWidth > P3_STATE_BITS_LEN)
188  FAIL(ILLEGAL_ARGUMENT);
189  return this->m_bits.Read(P3_STATE_BITS_POS + stateIndex, stateWidth);
190  }
191 
197  void SetStateField(u32 stateIndex, u32 stateWidth, u32 value)
198  {
199  if (stateWidth > P3_STATE_BITS_LEN)
200  FAIL(ILLEGAL_ARGUMENT);
201  return this->m_bits.Write(P3_STATE_BITS_POS + stateIndex, stateWidth, value);
202  }
203 
204  void PrintBits(ByteSink & ostream) const
205  { this->m_bits.Print(ostream); }
206 
207  void PrintImpl(ByteSink & ostream) const
208  {
209  u32 type = this->GetType();
210  ostream.Printf("P3[%x/",type);
211  u32 length = GetMaxStateSize(type);
212  for (u32 i = 0; i < length; i += 4) {
213  u32 nyb = this->GetStateField(i,4);
214  ostream.Printf("%x",nyb);
215  }
216  ostream.Printf("]");
217  }
218 
219  P3Atom& operator=(const P3Atom & rhs)
220  {
221  if (this == &rhs) return *this;
222 
223  this->m_bits = rhs.m_bits;
224 
225  return *this;
226  }
227 
228  };
229 } /* namespace MFM */
230 
231 #endif /*P3ATOM_H*/
static void Write(BV &bv, u32 val)
Definition: BitField.h:84
void Write(const u32 startIdx, const u32 length, const u32 value)
Definition: BitVector.tcc:99
void SetStateField(u32 stateIndex, u32 stateWidth, u32 value)
Definition: P3Atom.h:197
u32 GetType() const
Definition: Atom.h:178
static u32 Read(const BV &bv)
Definition: BitField.h:73
static u32 Add2DParity(u32 dataBits)
Definition: Parity2D_4x4.h:109
u32 Read(const u32 startIdx, const u32 length) const
Definition: BitVector.tcc:129
Definition: Atom.h:71
void WriteBit(u32 idx, bool bit)
Definition: BitVector.tcc:41
Definition: CoreConfig.h:40
void Print(ByteSink &ostream) const
Definition: BitVector.tcc:202
Definition: Atom.h:45
Definition: ByteSink.h:47
static u32 Correct2DParityIfPossible(u32 allBits)
Definition: Parity2D_4x4.cpp:18
bool ReadBit(u32 idx)
Definition: BitVector.tcc:65
u32 GetStateField(u32 stateIndex, u32 stateWidth) const
Definition: P3Atom.h:185
static bool Check2DParity(const u32 allBits)
Definition: Parity2D_4x4.h:145
Definition: P3Atom.h:44
BitVector< BPA > m_bits
Definition: Atom.h:99
u32 EndStateBit() const
Definition: P3Atom.h:151