Fletchgen
The Fletcher Design Generator
srec.h
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <cstdint>
18 #include <stdexcept>
19 #include <cstring>
20 #include <ostream>
21 #include <vector>
22 #include <memory>
23 #include <iostream>
24 #include <optional>
25 #include <string>
26 #include <sstream>
27 #include <iomanip>
28 
29 namespace fletchgen::srec {
30 
34 class Record {
35  public:
37  static constexpr size_t MAX_DATA_BYTES = 32;
38 
42  enum Type {
43  HEADER = 0,
44  DATA16 = 1,
45  DATA24 = 2,
46  DATA32 = 3,
47  RESERVED = 4,
48  COUNT16 = 5,
49  COUNT24 = 6,
50  TERM32 = 7,
51  TERM24 = 8,
52  TERM16 = 9
53  };
54 
56  Record(const Record &rec) : Record(rec.type_, rec.address_, rec.data_, rec.size_) {}
57 
65  Record(Type type, uint32_t address, const uint8_t *data, size_t size);
66 
68  ~Record();
69 
71  static std::optional<Record> FromString(const std::string &line);
72 
82  static Record Header(const std::string &header_str = "HDR", uint16_t address = 0);
83 
92  template<uint32_t S>
93  static Record Data(uint32_t srec_address, const uint8_t *data, size_t size) {
94  switch (S) {
95  case 16:return Record(DATA16, srec_address, data, size);
96  case 24:return Record(DATA24, srec_address, data, size);
97  case 32:return Record(DATA32, srec_address, data, size);
98  default:throw std::domain_error("SREC data records can only have 16, 24 or 32-bit address fields.");
99  }
100  }
101 
103  std::string ToString(bool line_feed = false);
104 
106  [[nodiscard]] inline uint32_t address() const { return address_; }
108  [[nodiscard]] inline size_t size() const { return size_; }
110  [[nodiscard]] inline uint8_t *data() const { return data_; }
111 
112  private:
114  Type type_ = RESERVED;
116  size_t size_ = 0;
118  uint32_t address_ = 0;
120  uint8_t *data_ = nullptr;
121 
123  int address_width();
125  uint8_t byte_count();
127  uint8_t checksum();
128 };
129 
130 inline void PutHex(std::stringstream &stream, uint32_t val, int characters = 2) {
131  stream << std::uppercase << std::hex << std::setfill('0') << std::setw(characters) << val;
132 }
133 
137 struct File {
138  File() = default;
139 
147  File(uint32_t start_address, const uint8_t *data, size_t size, const std::string &header_str = "HDR");
148 
153  explicit File(std::istream *input);
154 
159  void write(std::ostream *output);
160 
169  void ToBuffer(uint8_t **buffer, size_t *size);
170 
172  std::vector<Record> records;
173 };
174 
175 } // namespace fletchgen::srec
Structure to build up a single Record of an SREC file.
Definition: srec.h:34
static Record Header(const std::string &header_str="HDR", uint16_t address=0)
Create an SREC header Record.
Definition: srec.cc:44
Type
The SREC Record type.
Definition: srec.h:42
static constexpr size_t MAX_DATA_BYTES
Maximum number of data bytes per Record.
Definition: srec.h:37
Record(const Record &rec)
SREC Record copy constructor.
Definition: srec.h:56
size_t size() const
Return the size in bytes of this record.
Definition: srec.h:108
uint32_t address() const
Return the address of this record.
Definition: srec.h:106
uint8_t * data() const
Return the data source pointer of this record.
Definition: srec.h:110
std::string ToString(bool line_feed=false)
Return the SREC Record string.
Definition: srec.cc:84
static Record Data(uint32_t srec_address, const uint8_t *data, size_t size)
Create an SREC data Record.
Definition: srec.h:93
~Record()
Record destructor.
Definition: srec.cc:38
static std::optional< Record > FromString(const std::string &line)
Attempt to construct a Record from a string.
Definition: srec.cc:106
std::shared_ptr< Type > data(int width)
Fletcher data.
Definition: basic_types.cc:98
Structure to build up an SREC file with multiple Record lines.
Definition: srec.h:137
void ToBuffer(uint8_t **buffer, size_t *size)
Convert an SREC file to a raw buffer.
Definition: srec.cc:203
std::vector< Record > records
SREC records in this file.
Definition: srec.h:172
void write(std::ostream *output)
Write the SREC file to an output stream.
Definition: srec.cc:182