Previous 15994 Revisions Next

r15883 Monday 13th August, 2012 at 18:53:58 UTC by Curt Coder
[C64] Emulated the digital read circuitry of the Commodore 1541 disk drive according to the long board schematics. This will allow independent setting of the bitrate of incoming data and the sample rate. Passing more advanced copy protections require a cycle exact rewrite of the 6502 and VIA, but this is the first step toward proper emulation. [Curt Coder]
[src/mess/machine]64h156.c 64h156.h

src/mess/machine/64h156.c
r15882r15883
1111
1212    TODO:
1313
14    - serial IEC protocol handling
15    - write to disk image
14    http://staff.washington.edu/rrcc/uwweb/1541early/1540-2.GIF
1615
16   - move bit sync timer code into the execute loop
17   - analog read circuitry (phantom "weak" bits)
18   - model disk rotation for proper track alignment
19    - per-byte bit cell length
20    - write circuitry
21    - cycle exact M6502
22    - cycle exact VIA
23
1724*/
1825
1926#include "emu.h"
2027#include "64h156.h"
2128#include "formats/g64_dsk.h"
22#include "machine/devhelpr.h"
2329
2430
2531
r15882r15883
2935
3036#define LOG 0
3137
32#define SYNC \
33   !(m_oe && m_sync)
38#define ATN (m_atni ^ m_atna)
3439
35#define BYTE \
36   !(m_soe && m_byte)
3740
38#define ATN \
39   (m_atni ^ m_atna)
4041
41
42
4342//**************************************************************************
4443//  DEVICE DEFINITIONS
4544//**************************************************************************
r15882r15883
8584
8685
8786//-------------------------------------------------
88//  set_sync_line -
89//-------------------------------------------------
90
91inline void c64h156_device::set_sync_line()
92{
93   m_out_sync_func(SYNC);
94}
95
96
97//-------------------------------------------------
98//  set_byte_line -
99//-------------------------------------------------
100
101inline void c64h156_device::set_byte_line()
102{
103   m_out_byte_func(BYTE);
104}
105
106
107//-------------------------------------------------
10887//  read_current_track -
10988//-------------------------------------------------
11089
r15882r15883
122101   m_buffer_pos = 0;
123102   m_bit_pos = 7;
124103   m_bit_count = 0;
125}
126104
127
128//-------------------------------------------------
129//  receive_bit -
130//-------------------------------------------------
131
132inline void c64h156_device::receive_bit()
133{
134   if (m_bit_count == 1)
105   if (m_track_len)
135106   {
136      m_byte = 0;
137      set_byte_line();
107      m_speed = m_track_buffer[m_track_len] & 0x03;
108      m_bit_timer->adjust(attotime::zero, 0, attotime::from_hz(C2040_BITRATE[m_speed] / 4));
138109   }
139110
140   // shift in data from the read head
141   m_data <<= 1;
142   m_data |= BIT(m_track_buffer[m_buffer_pos], m_bit_pos);
143   m_bit_pos--;
144   m_bit_count++;
145
146   if (m_bit_pos < 0)
147   {
148      m_bit_pos = 7;
149      m_buffer_pos++;
150
151      if (m_buffer_pos >= m_track_len)
152      {
153         // loop to the start of the track
154         m_buffer_pos = 0;
155      }
156   }
157
158   m_sync = ((m_data & G64_SYNC_MARK) == G64_SYNC_MARK);
159   set_sync_line();
160
161   if (m_sync)
162   {
163      // SYNC detected
164      m_bit_count = 0;
165
166      if (LOG) logerror("SYNC\n");
167   }
168
169   if (m_bit_count > 7)
170   {
171      // byte ready
172      m_yb = m_data & 0xff;
173      m_bit_count = 0;
174
175      m_byte = 1;
176      set_byte_line();
177
178      if (LOG) logerror("BYTE %02x\n", m_yb);
179
180      if (!m_yb)
181      {
182         // simulate weak bits with randomness
183         m_yb = machine().rand() & 0xff;
184      }
185   }
111   m_bit_timer->enable(m_mtr && m_track_len);
186112}
187113
188114
r15882r15883
197123
198124c64h156_device::c64h156_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
199125    : device_t(mconfig, C64H156, "64H156", tag, owner, clock),
126      device_execute_interface(mconfig, *this),
127      m_icount(0),
200128     m_image(*this->owner(), FLOPPY_0),
201129     m_stp(-1),
202130     m_mtr(0),
r15882r15883
208136     m_accl(1),
209137     m_ds(-1),
210138     m_soe(0),
211     m_byte(0),
212139     m_oe(0),
213     m_sync(1),
214140     m_atni(1),
215     m_atna(1)
141     m_atna(1),
142     m_last_bit_sync(0),
143     m_bit_sync(0),
144     m_byte_sync(1),
145     m_block_sync(1),
146     m_ue7(0),
147     m_ue7_tc(0),
148     m_uf4(0),
149     m_uf4_qb(0),
150     m_ud2(0),
151     m_u4a(0),
152     m_u4b(0),
153     m_ue3(0),
154     m_uc1b(0)
216155{
217156}
218157
r15882r15883
223162
224163void c64h156_device::device_start()
225164{
165   // set our instruction counter
166   m_icountptr = &m_icount;
167
226168   // resolve callbacks
227169   m_out_atn_func.resolve(m_out_atn_cb, *this);
228170   m_out_sync_func.resolve(m_out_sync_cb, *this);
r15882r15883
239181   save_item(NAME(m_buffer_pos));
240182   save_item(NAME(m_bit_pos));
241183   save_item(NAME(m_bit_count));
242   save_item(NAME(m_data));
243   save_item(NAME(m_yb));
184   save_item(NAME(m_speed));
244185   save_item(NAME(m_accl));
245186   save_item(NAME(m_ds));
246187   save_item(NAME(m_soe));
247   save_item(NAME(m_byte));
248188   save_item(NAME(m_oe));
249   save_item(NAME(m_sync));
250189   save_item(NAME(m_atni));
251190   save_item(NAME(m_atna));
191   save_item(NAME(m_shift));
192   save_item(NAME(m_last_bit_sync));
193   save_item(NAME(m_bit_sync));
194   save_item(NAME(m_byte_sync));
195   save_item(NAME(m_block_sync));
196   save_item(NAME(m_ue7));
197   save_item(NAME(m_ue7_tc));
198   save_item(NAME(m_uf4));
199   save_item(NAME(m_uf4_qb));
200   save_item(NAME(m_ud2));
201   save_item(NAME(m_u4a));
202   save_item(NAME(m_u4b));
203   save_item(NAME(m_ue3));
204   save_item(NAME(m_uc1b));
252205}
253206
254207
r15882r15883
258211
259212void c64h156_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
260213{
261   receive_bit();
214   m_bit_sync = BIT(m_track_buffer[m_buffer_pos], m_bit_pos);
215
216   if (LOG) logerror("BIT SYNC %u\n", m_bit_sync);
217   m_shift <<= 1;
218   m_shift |= m_bit_sync;
219   
220   m_bit_pos--;
221   m_bit_count++;
222
223   if (m_bit_pos < 0)
224   {
225      m_bit_pos = 7;
226      m_buffer_pos++;
227
228      if (m_buffer_pos >= m_track_len)
229      {
230         // loop to the start of the track
231         m_buffer_pos = 0;
232      }
233   }
262234}
263235
264236
265237//-------------------------------------------------
238//  execute_run -
239//-------------------------------------------------
240
241void c64h156_device::execute_run()
242{
243   do
244   {
245      if (m_mtr)
246      {
247         // UE7
248         int ue7_tc = 0;
249
250         if (!m_last_bit_sync && m_bit_sync)
251         {
252            m_ue7 = m_ds;
253         }
254         else
255         {
256            m_ue7++;
257
258            if (m_ue7 == 16)
259            {
260               m_ue7 = m_ds;
261               ue7_tc = 1;
262            }
263         }
264
265         if (LOG) logerror("UE7 CTR %01x TC %u, ", m_ue7, ue7_tc);
266
267         // UF4
268         if (!m_last_bit_sync && m_bit_sync)
269         {
270            m_uf4 = 0;
271            if (LOG) logerror("--");
272         }
273         else
274         {
275            if (!m_ue7_tc && ue7_tc)
276            {
277               m_uf4++;
278
279               if (m_uf4 == 16)
280               {
281                  m_uf4 = 0;
282               }
283
284               if (LOG) logerror("++");
285            }
286            else if (LOG) logerror("  ");
287         }
288
289         m_last_bit_sync = m_bit_sync;
290         m_ue7_tc = ue7_tc;
291         
292         int uf4_qa = BIT(m_uf4, 0);
293         int uf4_qb = BIT(m_uf4, 1);
294         int uf4_qc = BIT(m_uf4, 2);
295         int uf4_qd = BIT(m_uf4, 3);
296
297         if (LOG) logerror("UF4 CTR %01x QD %u QC %u QB %u QA %u !QC|QD %u, ", m_uf4, uf4_qd, uf4_qc, uf4_qb, uf4_qa, !(uf4_qc || uf4_qd));
298
299         if (!m_uf4_qb && uf4_qb)
300         {
301            if (LOG) logerror("<<");
302            // shift bits thru flip-flops
303            m_u4b = m_u4a;
304            m_u4a = BIT(m_ud2, 7);
305
306            // shift in data bit
307            m_ud2 <<= 1;
308            m_ud2 |= !(uf4_qc || uf4_qd);
309         }
310         else
311         {
312            if (LOG) logerror("  ");
313         }
314
315         if (LOG) logerror("UD2 %u%u%u%u%u%u%u%u%u%u : %02x (%02x), ", m_u4b, m_u4a, BIT(m_ud2, 7), BIT(m_ud2, 6), BIT(m_ud2, 5), BIT(m_ud2, 4), BIT(m_ud2, 3), BIT(m_ud2, 2), BIT(m_ud2, 1), BIT(m_ud2, 0), m_ud2, m_shift & 0xff);
316
317         int block_sync = !(m_oe && (m_ud2 == 0xff) && m_u4a && m_u4b);
318         
319         if (LOG) logerror("SYNC %u, ", block_sync);
320
321         if (!block_sync)
322         {
323            // load UE3
324            m_ue3 = 8; // pin D is floating and TTL inputs float high
325            if (LOG) logerror("--");
326         }
327         else
328         {
329            if (m_block_sync && !m_uf4_qb && uf4_qb)
330            {
331               // clock UE3
332               m_ue3++;
333               if (LOG) logerror("++");
334               
335               if (m_ue3 == 16)
336               {
337                  m_ue3 = 0;
338               }
339            }
340            else
341            {
342               if (LOG) logerror("  ");
343            }
344         }
345
346         m_uf4_qb = uf4_qb;
347
348         int ue3_qa = BIT(m_ue3, 0);
349         int ue3_qb = BIT(m_ue3, 1);
350         int ue3_qc = BIT(m_ue3, 2);
351
352         int uf3a = !(ue3_qa && ue3_qb && ue3_qc);
353         int uc1b = !uf3a; // schmitt trigger
354
355         int byte_sync = !(uc1b && m_soe && !uf4_qb);
356
357         if (LOG) logerror("UE3 CTR %01x UF3A %u UC1B %u, ", m_ue3, uf3a, uc1b);
358
359         if (LOG) logerror("BYTE %u SOE %u\n", m_byte_sync, m_soe);
360         
361         if (m_block_sync != block_sync)
362         {
363            m_block_sync = block_sync;
364            m_out_sync_func(m_block_sync);
365         }
366         
367         if (m_byte_sync != byte_sync)
368         {
369            m_byte_sync = byte_sync;
370            m_out_byte_func(m_byte_sync);
371         }
372
373         m_bit_sync = 0;
374      }
375
376      m_icount--;
377   } while (m_icount > 0);
378}
379
380
381//-------------------------------------------------
266382//  yb_r -
267383//-------------------------------------------------
268384
r15882r15883
270386{
271387   UINT8 data = 0;
272388
273   if (LOG) logerror("YB read %02x\n", m_yb);
274
275389   if (m_soe)
276390   {
277      data = m_yb;
391      if (!m_ud2)
392      {
393         data = rand() & 0xff;
394      }
395      else
396      {
397         data = m_ud2;
398      }
278399   }
279400
401   if (LOG) logerror("YB read %02x:%02x\n", m_ud2, data);
402
280403   return data;
281404}
282405
r15882r15883
287410
288411WRITE8_MEMBER( c64h156_device::yb_w )
289412{
290   //m_yb = data;
291413}
292414
293415
r15882r15883
316438
317439READ_LINE_MEMBER( c64h156_device::sync_r )
318440{
319   return SYNC;
441   return m_block_sync;
320442}
321443
322444
r15882r15883
326448
327449READ_LINE_MEMBER( c64h156_device::byte_r )
328450{
329   return BYTE;
451   return m_byte_sync;
330452}
331453
332454
r15882r15883
338460{
339461   if (m_mtr != state)
340462   {
463      if (LOG) logerror("MTR %u\n", state);
464
341465      if (state)
342466      {
343467         // read track data
r15882r15883
345469      }
346470
347471      floppy_mon_w(m_image, !state);
348      m_bit_timer->enable(state);
472      m_bit_timer->enable(state && m_track_len);
349473
350474      m_mtr = state;
351475   }
r15882r15883
358482
359483WRITE_LINE_MEMBER( c64h156_device::oe_w )
360484{
485   if (LOG) logerror("OE %u\n", state);
486   
361487   m_oe = state;
362
363   set_sync_line();
364488}
365489
366490
r15882r15883
373497   if (LOG) logerror("SOE %u\n", state);
374498
375499   m_soe = state;
376
377   set_byte_line();
378500}
379501
380502
r15882r15883
454576
455577void c64h156_device::ds_w(int data)
456578{
457   if (m_ds != data)
458   {
459      m_bit_timer->adjust(attotime::zero, 0, attotime::from_hz(C2040_BITRATE[data]/4));
460      m_ds = data;
461   }
579   m_ds = data;
462580}
463581
464582
r15882r15883
473591
474592
475593//-------------------------------------------------
476//  on_disk_changed -
594//  set_side -
477595//-------------------------------------------------
478596
479597void c64h156_device::set_side(int side)
src/mess/machine/64h156.h
r15882r15883
9797// ======================> c64h156_device
9898
9999class c64h156_device :  public device_t,
100                  public device_execute_interface,
100101                  public c64h156_interface
101102{
102103public:
r15882r15883
124125
125126protected:
126127    // device-level overrides
128    virtual void device_config_complete();
127129    virtual void device_start();
128130   virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
129    virtual void device_config_complete();
130131
132    // device_execute_interface overrides
133   virtual void execute_run();
134
135    int m_icount;
136
131137   inline void set_atn_line();
132   inline void set_sync_line();
133   inline void set_byte_line();
134138   inline void read_current_track();
135   inline void receive_bit();
136139
137140private:
138141   devcb_resolved_write_line   m_out_atn_func;
r15882r15883
152155   offs_t m_buffer_pos;               // current byte position within track buffer
153156   int m_bit_pos;                     // current bit position within track buffer byte
154157   int m_bit_count;                  // current data byte bit counter
155   UINT16 m_data;                     // data shift register
156   UINT8 m_yb;                        // GCR data byte
158   UINT8 m_speed;
157159
158160   // signals
159161   int m_accl;                        // 1/2 MHz select
160162   int m_ds;                        // density select
161163   int m_soe;                        // s? output enable
162   int m_byte;                        // byte ready
163164   int m_oe;                        // output enable (0 = write, 1 = read)
164   int m_sync;                        // sync character found
165165
166166   // IEC
167167   int m_atni;                        // attention input
168168   int m_atna;                        // attention acknowledge
169169
170   // logic
171   UINT16 m_shift;
172   int m_last_bit_sync;
173   int m_bit_sync;
174   int m_byte_sync;
175   int m_block_sync;
176   int m_ue7;
177   int m_ue7_tc;
178   int m_uf4;
179   int m_uf4_qb;
180   UINT8 m_ud2;
181   int m_u4a;
182   int m_u4b;
183   int m_ue3;
184   int m_uc1b;
185
170186   // timers
171187   emu_timer *m_bit_timer;
172188

Previous 15994 Revisions Next


© 1998-2018 The MESS Team