• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • kasten
  • gui
  • io
  • streamencoder
  • srec
bytearraysrecstreamencoder.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Okteta Kasten module, made within the KDE community.
3 
4  Copyright 2010 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) version 3, or any
10  later version accepted by the membership of KDE e.V. (or its
11  successor approved by the membership of KDE e.V.), which shall
12  act as a proxy defined in Section 6 of version 3 of the license.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "bytearraysrecstreamencoder.h"
24 
25 // lib
26 #include <bytearrayview.h>
27 // Okteta gui
28 #include <bytearraytablelayout.h>
29 // Okteta core
30 #include <abstractbytearraymodel.h>
31 // KDE
32 #include <KLocale>
33 // Qt
34 #include <QtCore/QTextStream>
35 
36 
37 namespace Kasten2
38 {
39 
40 SRecStreamEncoderSettings::SRecStreamEncoderSettings()
41  : addressSizeId( FourBytesId )
42 {}
43 
44 const char ByteArraySRecStreamEncoder::hexDigits[16] =
45 { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
46 
47 
48 void ByteArraySRecStreamEncoder::streamLine( QTextStream& textStream, RecordType recordType,
49  const unsigned char* line )
50 {
51  // checksum is ones' complement of sum of the values in the line
52  unsigned char checksum = 0;
53 
54  textStream << startCode << charOfRecordType(recordType);
55 
56  const uint length = line[0];
57  for( uint i = 0; i < length; ++i )
58  {
59  const unsigned char byte = line[i];
60  textStream << hexValueOfNibble( byte >> 4 )
61  << hexValueOfNibble( byte );
62  checksum += byte;
63  }
64 
65  checksum = ~checksum;
66  textStream << hexValueOfNibble( checksum >> 4 ) << hexValueOfNibble( checksum ) << '\n';
67 }
68 
69 
70 void ByteArraySRecStreamEncoder::streamBlockHeader( QTextStream& textStream, unsigned char* line )
71 // const char* moduleName = 0, const char* description = 0,
72 // quint8 version = 0, quint8 revision = 0 )
73 {
74  // cmp. http://linux.die.net/man/5/srec
75  // WP says: vendor specific data rather than program data
76 // static const int moduleNameLineOffset = 3;
77 // static const int moduleNameLength = 10;
78 // static const int versionLineOffset = moduleNameLineOffset + moduleNameLength;
79 // static const int versionLength = 1;
80 // static const int revisionLineOffset = versionLineOffset + versionLength;
81 // static const int revisionLength = 1;
82 // static const int descriptionLineOffset = revisionLineOffset + revisionLength;
83 // static const int descriptionLength = 18;
84  static const int headerByteCount = 3;
85 
86  line[addressLineOffset] = 0; // address unused
87  line[addressLineOffset+1] = 0; // address unused
88 
89  // leave data empty for now
90  line[byteCountLineOffset] = headerByteCount;
91 
92  streamLine( textStream, BlockHeader, line );
93 }
94 
95 void ByteArraySRecStreamEncoder::streamRecordCount( QTextStream& textStream, unsigned char* line,
96  quint16 recordCount )
97 {
98  static const int recordCountLineSize = 2;
99  static const int recordCountByteCount = byteCountLineSize + recordCountLineSize;
100 
101  line[byteCountLineOffset] = recordCountByteCount;
102  writeBigEndian( &line[addressLineOffset], recordCount, recordCountLineSize );
103 
104  streamLine( textStream, RecordCount, line );
105 }
106 
107 // from M68000PRM.pdf:
108 //comp. with tty 28 bytes are max (with 3b address)
109 // terminated with CR if downloading
110 // s-record may have some initial field, e.g. for line-number
111 // end of x-block: The address field may optionally contain the x-byte address of the instruction to which control is to be passed.
112 // Under VERSAdos, the resident linkerOs ENTRY command can be used to
113 // specify this address. If this address is not specified, the first entry point speci-
114 // fication encountered in the object module input will be used. There is no code/
115 // data field.
116 
117 // TODO: recordType is not limited to valid values, also brings recalculation of addressLineSize
118 void ByteArraySRecStreamEncoder::streamBlockEnd( QTextStream& textStream, unsigned char* line,
119  RecordType recordType, quint32 startAddress )
120 {
121  const int addressLineSize = 11 - recordType;
122  const int blockEndByteCount = byteCountLineSize + addressLineSize;
123 
124  line[byteCountLineOffset] = blockEndByteCount;
125  writeBigEndian( &line[addressLineOffset], startAddress, addressLineSize );
126 
127  streamLine( textStream, recordType, line );
128 }
129 
130 
131 ByteArraySRecStreamEncoder::ByteArraySRecStreamEncoder()
132  : AbstractByteArrayStreamEncoder( i18nc("name of the encoding target","S-Record"), QString::fromLatin1("text/x-srecord") )
133 {}
134 
135 
136 bool ByteArraySRecStreamEncoder::encodeDataToStream( QIODevice* device,
137  const ByteArrayView* byteArrayView,
138  const Okteta::AbstractByteArrayModel* byteArrayModel,
139  const Okteta::AddressRange& range )
140 {
141  Q_UNUSED( byteArrayView );
142 
143  bool success = true;
144 
145  // encode
146  QTextStream textStream( device );
147 
148  // prepare
149  static const int maxLineLength = 64 / 2;
150  const int addressLineSize = 4 - mSettings.addressSizeId;
151  const int maxDataPerLineCount = maxLineLength - byteCountLineSize - addressLineSize;
152  const int dataLineOffset = addressLineOffset + addressLineSize;
153 
154  const Okteta::ByteArrayTableLayout layout( byteArrayView->noOfBytesPerLine(),
155  byteArrayView->firstLineOffset(),
156  byteArrayView->startOffset(), 0, byteArrayModel->size() );
157 
158  const Okteta::Coord startCoord = layout.coordOfIndex( range.start() );
159  const int lastLinePosition = layout.lastLinePosition( startCoord.line() );
160  const int dataPerLineCount = qMin( byteArrayView->noOfBytesPerLine(), maxDataPerLineCount );
161  const RecordType dataSequenceType =
162  static_cast<RecordType>( DataSequence4B - mSettings.addressSizeId );
163  const RecordType endOfBlockType =
164  static_cast<RecordType>( EndOfBlock4B + mSettings.addressSizeId );
165 
166  unsigned char line[maxLineLength];
167 
168  unsigned char* const lineData = &line[dataLineOffset];
169  const int firstDataEnd = lastLinePosition - startCoord.pos() + 1;
170  int d = 0;
171  int nextDataEnd = qMin( firstDataEnd, dataPerLineCount );
172  Okteta::Address recordOffset = range.start();
173  int recordCount = 0;
174 
175  // header
176  streamBlockHeader( textStream, line );
177 
178  Okteta::Address i = range.start();
179  while( i<=range.end() )
180  {
181  const Okteta::Byte byte = byteArrayModel->byte( i );
182  lineData[d] = byte;
183 
184  ++d;
185  ++i;
186  if( d == nextDataEnd )
187  {
188  line[byteCountLineOffset] = d + 1 + addressLineSize;
189  writeBigEndian( &line[addressLineOffset], recordOffset, addressLineSize );
190 
191  streamLine( textStream, dataSequenceType, line );
192 
193  ++recordCount;
194  recordOffset = i;
195  d = 0;
196  nextDataEnd = qMin( range.end()-i+1, dataPerLineCount );
197  }
198  }
199 
200  // footer
201  streamRecordCount( textStream, line, recordCount );
202  streamBlockEnd( textStream, line, endOfBlockType );
203 
204  return success;
205 }
206 
207 ByteArraySRecStreamEncoder::~ByteArraySRecStreamEncoder() {}
208 
209 }
Okteta::Address
qint32 Address
Definition: address.h:34
Okteta::AbstractByteArrayModel
could it be useful to hide the data access behind an iterator? * class KDataBufferIterator { public: ...
Definition: abstractbytearraymodel.h:79
abstractbytearraymodel.h
Kasten2::ByteArrayView::noOfBytesPerLine
int noOfBytesPerLine() const
Definition: bytearrayview.cpp:244
KDE::NumberRange< Address, Size >
KDE::Range::start
T start() const
Definition: range.h:86
Okteta::Byte
unsigned char Byte
Definition: byte.h:29
Okteta::Coord
a class which represents a coord in a 2-dim.
Definition: coord.h:47
Kasten2::ByteArrayView::firstLineOffset
Okteta::Address firstLineOffset() const
Definition: bytearrayview.cpp:240
Kasten2::AbstractByteArrayStreamEncoder
Definition: abstractbytearraystreamencoder.h:45
Kasten2::ByteArraySRecStreamEncoder::~ByteArraySRecStreamEncoder
virtual ~ByteArraySRecStreamEncoder()
Definition: bytearraysrecstreamencoder.cpp:207
bytearraytablelayout.h
Okteta::ByteArrayTableLayout
the logical layout of a byte array table for a view
Definition: bytearraytablelayout.h:61
Okteta::AbstractByteArrayModel::size
virtual Size size() const =0
KDE::Range::end
T end() const
Definition: range.h:88
Kasten2::SRecStreamEncoderSettings::addressSizeId
AddressSizeId addressSizeId
Definition: bytearraysrecstreamencoder.h:47
Kasten2::ByteArraySRecStreamEncoder::encodeDataToStream
virtual bool encodeDataToStream(QIODevice *device, const ByteArrayView *byteArrayView, const Okteta::AbstractByteArrayModel *byteArrayModel, const Okteta::AddressRange &range)
Definition: bytearraysrecstreamencoder.cpp:136
Kasten2::ByteArraySRecStreamEncoder::ByteArraySRecStreamEncoder
ByteArraySRecStreamEncoder()
Definition: bytearraysrecstreamencoder.cpp:131
Okteta::Coord::pos
LinePosition pos() const
Definition: coord.h:213
Okteta::AbstractByteArrayModel::byte
virtual Byte byte(Address offset) const =0
locates working range The idea behind is to tell buffer which range will be requested in the followin...
Kasten2::SRecStreamEncoderSettings::SRecStreamEncoderSettings
SRecStreamEncoderSettings()
Definition: bytearraysrecstreamencoder.cpp:40
Kasten2::ByteArraySRecStreamEncoder::mSettings
SRecStreamEncoderSettings mSettings
Definition: bytearraysrecstreamencoder.h:103
bytearraysrecstreamencoder.h
Kasten2::ByteArrayView::startOffset
Okteta::Address startOffset() const
Definition: bytearrayview.cpp:236
QIODevice
Kasten2::ByteArrayView
Definition: bytearrayview.h:51
bytearrayview.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:07 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal