/[projet1]/public/pc/tools/oric/Bin2Tap/OricBinaryTapeImage.cpp
Defence Force logotype

Contents of /public/pc/tools/oric/Bin2Tap/OricBinaryTapeImage.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 315 - (show annotations)
Tue Apr 27 13:27:38 2010 UTC (9 years, 6 months ago) by Vampire
File size: 3519 byte(s)


1 #include "StdAfx.h"
2 #include "OricBinaryTapeImage.h"
3
4 COricBinaryTapeImage::COricBinaryTapeImage(void)
5 {
6 m_Buffer = NULL;
7
8 // Create the header
9 memset( &m_Header, 0, sizeof(m_Header));
10 m_Header.m_Type = 0x80;
11 m_Header.m_AutoStart = 0x00;
12
13 m_OricFilename = "MCODE";
14 }
15
16 COricBinaryTapeImage::~COricBinaryTapeImage(void)
17 {
18 delete [] m_Buffer;
19 }
20
21 bool COricBinaryTapeImage::LoadBinary( CString filename)
22 {
23 CFile binary_file;
24
25 if ( !binary_file.Open( filename, CFile::modeRead))
26 {
27 return false;
28 }
29
30 // Calculate the buffer length
31 m_BufferLength = (int)binary_file.GetLength() - 2;
32 m_Buffer = new byte[m_BufferLength];
33
34 // Get the load and end addresses
35 unsigned short org_address;
36 binary_file.Read( &org_address, 2);
37 int end = (org_address + m_BufferLength) - 1;
38
39 // Create the header
40 m_Header.m_LowStart = org_address & 0xff;
41 m_Header.m_HighStart = org_address >> 8;
42 m_Header.m_LowEnd = end & 0xff;
43 m_Header.m_HighEnd = end >> 8;
44
45 binary_file.Read( m_Buffer, m_BufferLength);
46
47 binary_file.Close();
48
49 return true;
50 }
51
52 bool COricBinaryTapeImage::WriteWave( CPulseWaveWriter *wave_writer) const
53 {
54 int i;
55
56 wave_writer->WriteBlank( 0.5f);
57
58 for ( i = 0; i < 16; i++)
59 {
60 WriteByte( wave_writer, 0x16);
61 }
62 WriteByte( wave_writer, 0x24);
63
64 byte *out_pointer = (byte *)&m_Header;
65 for ( i = 0; i < sizeof(m_Header); i++)
66 {
67 WriteByte( wave_writer, *(out_pointer++));
68 }
69
70 int l = m_OricFilename.GetLength();
71 char* ascii = new char[l + 1];
72 wcstombs_s(NULL, ascii, l + 1, (LPCTSTR)m_OricFilename, l );
73
74 out_pointer = (byte *)ascii;
75 for ( i = 0; i <= m_OricFilename.GetLength(); i++)
76 {
77 WriteByte( wave_writer, *(out_pointer++));
78 }
79
80 delete [] ascii;
81
82 for ( int i = 0; i < 10; i++)
83 {
84 wave_writer->WritePulse( false);
85 }
86
87 out_pointer = (byte *)m_Buffer;
88 for ( i = 0; i < (int)m_BufferLength; i++)
89 {
90 WriteByte( wave_writer, *(out_pointer++));
91 }
92
93 wave_writer->WriteBlank( 0.5f);
94
95 return true;
96 }
97
98 bool COricBinaryTapeImage::WriteTape( CTapeWriter *tape_writer) const
99 {
100 int i;
101
102 WriteByte( tape_writer, 0x16);
103 WriteByte( tape_writer, 0x24);
104
105 byte *out_pointer = (byte *)&m_Header;
106 for ( i = 0; i < sizeof(m_Header); i++)
107 {
108 WriteByte( tape_writer, *(out_pointer++));
109 }
110
111 int l = m_OricFilename.GetLength();
112 char* ascii = new char[l + 1];
113 wcstombs_s(NULL, ascii, l + 1, (LPCTSTR)m_OricFilename, l );
114
115 out_pointer = (byte *)ascii;
116 for ( i = 0; i <= m_OricFilename.GetLength(); i++)
117 {
118 WriteByte( tape_writer, *(out_pointer++));
119 }
120
121 delete [] ascii;
122
123 out_pointer = (byte *)m_Buffer;
124 for ( i = 0; i < (int)m_BufferLength; i++)
125 {
126 WriteByte( tape_writer, *(out_pointer++));
127 }
128
129 return true;
130 }
131
132 void COricBinaryTapeImage::WritePair( CPulseWaveWriter *wave_writer, bool long_pulse) const
133 {
134 wave_writer->WritePulse( false);
135 wave_writer->WritePulse( long_pulse);
136 }
137
138 void COricBinaryTapeImage::WriteByte( CPulseWaveWriter *wave_writer, byte out_byte) const
139 {
140 // Push the start bit
141 WritePair( wave_writer, true);
142
143 unsigned char crc = 1;
144 for ( int i = 0; i < 8; i++)
145 {
146 unsigned char bit = out_byte & 1;
147 out_byte >>= 1;
148 WritePair( wave_writer, bit == 0);
149 crc ^= bit;
150 }
151
152 WritePair( wave_writer, crc == 0);
153
154 for ( int i = 0; i < 7; i++)
155 {
156 wave_writer->WritePulse( false);
157 }
158 }
159
160 void COricBinaryTapeImage::WriteByte( CTapeWriter *tape_writer, byte out_byte) const
161 {
162 tape_writer->WriteByte( out_byte);
163 }

  ViewVC Help
Powered by ViewVC 1.1.26