OpenASIP  2.0
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
Bin2nImageWriter Class Reference

#include <Bin2nImageWriter.hh>

Inheritance diagram for Bin2nImageWriter:
Inheritance graph
Collaboration diagram for Bin2nImageWriter:
Collaboration graph

Public Member Functions

 Bin2nImageWriter (const BitVector &bits, int rowLength)
 
virtual ~Bin2nImageWriter ()
 
virtual void writeImage (std::ostream &stream) const
 
const BitVectorbits () const
 
void writeSequence (std::ostream &stream, unsigned int length) const
 
- Public Member Functions inherited from BitImageWriter
virtual ~BitImageWriter ()
 

Private Member Functions

unsigned int nextPowerOf2 (unsigned int n) const
 

Static Private Member Functions

static char character (const BitVector &bits, unsigned int startIndex, int length)
 

Private Attributes

const BitVectorbits_
 The bits to be written. More...
 
int rowLength_
 The length of a row in the output. More...
 
unsigned int nextBitIndex_
 The index of the next bit to be written. More...
 

Detailed Description

Writes the data image with binary 1's and 0's. Pads from the left to next multiple of 2 of rowLength_ and after that reverses the byte endianness. Used for programming the FPGA with Jupyter through AlmaIF

Definition at line 46 of file Bin2nImageWriter.hh.

Constructor & Destructor Documentation

◆ Bin2nImageWriter()

Bin2nImageWriter::Bin2nImageWriter ( const BitVector bits,
int  rowLength 
)

Definition at line 40 of file Bin2nImageWriter.cc.

41  :
42  bits_(bits), rowLength_(rowLength), nextBitIndex_(0) {
43 }

◆ ~Bin2nImageWriter()

Bin2nImageWriter::~Bin2nImageWriter ( )
virtual

Definition at line 45 of file Bin2nImageWriter.cc.

45  {
46 }

Member Function Documentation

◆ bits()

const BitVector & Bin2nImageWriter::bits ( ) const

Definition at line 157 of file Bin2nImageWriter.cc.

157  {
158  return bits_;
159 }

References bits_.

Referenced by character(), and Bin2nProgramImageWriter::writeImage().

◆ character()

char Bin2nImageWriter::character ( const BitVector bits,
unsigned int  startIndex,
int  length 
)
staticprivate

Returns the byte of the given bit vector that starts at the given index and is 'length' bits long. Pads from the left to size 8.

Parameters
bitsThe bit vector.
startIndexThe start index.
lengthHow many indexes forward is read. 0-8.
Returns
The byte.

Pad from the left with zeros.

Read the first bit.

Read the rest of the bits.

Definition at line 77 of file Bin2nImageWriter.cc.

77  {
78  unsigned int vectorSize = bits.size();
79  char character = 0;
80 
81  /// Pad from the left with zeros.
82  character = character << (8 - length);
83 
84  /// Read the first bit.
85  if (bits[startIndex] && length > 0){
86  character++;
87  }
88 
89  /// Read the rest of the bits.
90  for (int i = 1; i < length; i++) {
91  character = character << 1;
92  if (startIndex + i < vectorSize && bits[startIndex + i]) {
93  character++;
94  }
95  }
96  return character;
97 }

References bits().

Referenced by writeSequence().

Here is the call graph for this function:

◆ nextPowerOf2()

unsigned int Bin2nImageWriter::nextPowerOf2 ( unsigned int  n) const
private

Returns the next power of 2 of the given parameter

Parameters
n
Returns
p the next power of 2 of n

Definition at line 168 of file Bin2nImageWriter.cc.

168  {
169 
170  unsigned int p = 1;
171 
172  if (!(n & (n - 1)))
173  return n;
174 
175  while(p < n) {
176  p <<= 1;
177  }
178 
179  return p;
180 }

Referenced by writeSequence().

◆ writeImage()

void Bin2nImageWriter::writeImage ( std::ostream &  stream) const
virtual

Writes the data bits to the given stream. Reverses the byte endianness

Parameters
streamThe output stream.

Bitvector-size has to always be exactly divisible by rowlength

Implements BitImageWriter.

Reimplemented in Bin2nProgramImageWriter.

Definition at line 54 of file Bin2nImageWriter.cc.

54  {
55 
56  unsigned int size = bits_.size();
57 
58  /// Bitvector-size has to always be exactly divisible by rowlength
59  unsigned int numOfRows = size / rowLength_;
60 
61  for (unsigned int i = 0; i < numOfRows; i++){
62  writeSequence(stream, rowLength_);
63 
64  }
65 }

References bits_, rowLength_, and writeSequence().

Here is the call graph for this function:

◆ writeSequence()

void Bin2nImageWriter::writeSequence ( std::ostream &  stream,
unsigned int  length 
) const

Writes a sequence of bits to the given stream.

When this method is called sequentially, the first bit to be written is the next bit to the last bit written in the previous method call. Pads the sequence from the left with zeroes up to the nearest multiple of 2. After that, reverses the string before writing it to the stream.

Parameters
streamThe output stream.
lengthThe length of the sequence to be written.

Pad from the left the correct amount of "0 bytes".

There might be a partial byte, which needs to be padded from the left

If there is no partial byte, we don't want to insert anything

Create the full bytes.

Update this for the next iteration of the function.

Reverse the byte endianness and write the stream.

Definition at line 112 of file Bin2nImageWriter.cc.

112  {
113 
114  if (length == 0){
115  return;
116  }
117 
118  unsigned int lastIndex = nextBitIndex_ + length -1;
119  unsigned int imem_width = nextPowerOf2(length);
120  std::string sequence_word = "";
121 
122  /// Pad from the left the correct amount of "0 bytes".
123  for (unsigned int i = 0;i < (imem_width-length)/8; i++){
124  char zero = 0;
125  sequence_word.push_back(zero);
126  }
127 
128  /// There might be a partial byte, which needs to be padded from the left
129  unsigned int partial_start_index = nextBitIndex_;
130  unsigned int partial_char_len = length % 8;
131 
132  /// If there is no partial byte, we don't want to insert anything
133  if (partial_char_len != 0){
134  sequence_word.push_back(character(bits_, partial_start_index, partial_char_len));
135  partial_start_index += partial_char_len;
136  }
137 
138  /// Create the full bytes.
139  for (unsigned int index = partial_start_index; index <= lastIndex; index +=8){
140 
141  sequence_word.push_back(character(bits_, index, 8));
142 
143  }
144 
145  /// Update this for the next iteration of the function.
146  nextBitIndex_ = lastIndex + 1;
147 
148  /// Reverse the byte endianness and write the stream.
149  for (std::string::reverse_iterator iter = sequence_word.rbegin();
150  iter != sequence_word.rend(); iter++){
151 
152  stream << *iter;
153  }
154 }

References bits_, character(), nextBitIndex_, and nextPowerOf2().

Referenced by Bin2nProgramImageWriter::writeImage(), and writeImage().

Here is the call graph for this function:

Member Data Documentation

◆ bits_

const BitVector& Bin2nImageWriter::bits_
private

The bits to be written.

Definition at line 63 of file Bin2nImageWriter.hh.

Referenced by bits(), writeImage(), and writeSequence().

◆ nextBitIndex_

unsigned int Bin2nImageWriter::nextBitIndex_
mutableprivate

The index of the next bit to be written.

Definition at line 67 of file Bin2nImageWriter.hh.

Referenced by writeSequence().

◆ rowLength_

int Bin2nImageWriter::rowLength_
private

The length of a row in the output.

Definition at line 65 of file Bin2nImageWriter.hh.

Referenced by writeImage().


The documentation for this class was generated from the following files:
Bin2nImageWriter::nextPowerOf2
unsigned int nextPowerOf2(unsigned int n) const
Definition: Bin2nImageWriter.cc:168
Bin2nImageWriter::bits
const BitVector & bits() const
Definition: Bin2nImageWriter.cc:157
Bin2nImageWriter::rowLength_
int rowLength_
The length of a row in the output.
Definition: Bin2nImageWriter.hh:65
Bin2nImageWriter::bits_
const BitVector & bits_
The bits to be written.
Definition: Bin2nImageWriter.hh:63
Bin2nImageWriter::nextBitIndex_
unsigned int nextBitIndex_
The index of the next bit to be written.
Definition: Bin2nImageWriter.hh:67
Bin2nImageWriter::character
static char character(const BitVector &bits, unsigned int startIndex, int length)
Definition: Bin2nImageWriter.cc:77
Bin2nImageWriter::writeSequence
void writeSequence(std::ostream &stream, unsigned int length) const
Definition: Bin2nImageWriter.cc:112