1 /*
2  *  KCemu -- The emulator for the KC85 homecomputer series and much more.
3  *  Copyright (C) 1997-2010 Torsten Paul
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef __kc_tape_h
21 #define __kc_tape_h
22 
23 #include <fstream>
24 
25 #include "kc/system.h"
26 
27 #include "kc/cb.h"
28 
29 #include "cmd/cmd.h"
30 
31 #include "libtape/kct.h"
32 
33 #include "fileio/load.h"
34 
35 typedef enum
36 {
37   TAPE_OK,
38   TAPE_OK_READONLY,
39   TAPE_NOENT,
40   TAPE_ERROR,
41 } tape_error_t;
42 
43 /*
44  *
45  *  system clock 1.75 Mhz (KC 85/3, KC 85/4)
46  *
47  *  const int BIT_0 =  364;  - 2400 Hz
48  *  const int BIT_1 =  729;  - 1200 Hz
49  *  const int BIT_S = 1458;  -  600 Hz
50  *
51  *  system clock 2.5 Mhz (Z9001, KC 85/1, KC87)
52  *
53  *  const int BIT_0 =  520;  - 2400 Hz
54  *  const int BIT_1 = 1041;  - 1200 Hz
55  *  const int BIT_S = 2083;  -  600 Hz
56  */
57 
58 class TapeCallback {
59  public:
TapeCallback(void)60   TapeCallback(void) {}
~TapeCallback(void)61   virtual ~TapeCallback(void) {}
62 
63   virtual void tape_callback(byte_t val) = 0;
64 };
65 
66 class Tape : public Callback
67 {
68 public:
69   enum {
70     BLOCK_SIZE = 131,
71   };
72 
73 private:
74     int BIT_0;
75     int BIT_1;
76     int BIT_S;
77     int _start_block;
78 
79     bool _power;
80     bool _play;
81     bool _record;
82 
83     int _flip_flop;
84     int _sync;
85     int _sync_count;
86     int _bits;
87     int _state;
88     int _block;
89     int _byte_counter;
90     int _init;
91 
92     byte_t _byte;
93     byte_t _crc;
94     byte_t _crc_calculated;
95     byte_t _buf[129];
96     byte_t _last_val;
97     byte_t _last_block;
98 
99     long _file_size;
100     long _bytes_read;
101     kct_file_type_t _file_type;
102 
103     std::ostream *_os;
104     std::istream *_is;
105 
106     KCTFile _kct_file;
107 
108     TapeCallback *_tape_cb;
109 
110     CMD *_cmd_tape_load;
111     CMD *_cmd_tape_play;
112     CMD *_cmd_tape_attach;
113     CMD *_cmd_tape_export;
114     CMD *_cmd_tape_add_file;
115 
116  protected:
117     void update_tape_list(void);
118     bool check_addr(byte_t *data, long size);
119     bool check_com(byte_t *data, long size);
120     const char *get_filename(byte_t *data);
121 
122  public:
123     Tape(int bit_0, int bit_1, int bit_s, int start_block);
124     virtual ~Tape(void);
125 
126     void power(bool val);
127     void record(void);
128     void play(const char *name, int delay);
129     void stop(void);
130     void seek(int percent);
131     void do_play(int edge);
132     void do_play_bic(int edge);
133     void do_play_z1013(int edge);
134     void do_play_basicode(int edge);
135     void do_stop(void);
136     void tape_signal(void);
137     void tape_signal_bic(long diff);
138     void tape_signal_z1013(long diff);
139     long get_delay(int seconds);
140 
141     virtual void set_tape_callback(TapeCallback *tape_cb);
142 
143     virtual void callback(void *data);
144 
145     virtual tape_error_t attach(const char *filename, bool create = false);
146     virtual tape_error_t detach(void);
147     virtual tape_error_t add(const char *name);
148     virtual tape_error_t add_raw(const char *filename,
149                                  const char *tape_filename,
150                                  const char *kc_filename,
151                                  unsigned short load,
152                                  unsigned short start,
153                                  bool autostart);
154     virtual tape_error_t add_file(const char *name,
155 				  fileio_prop_t *prop,
156 				  kct_file_type_t type,
157 				  kct_machine_type_t machine);
158     virtual tape_error_t rename(const char *from, const char *to);
159     virtual tape_error_t remove(const char *name);
160     virtual tape_error_t export_tap(const char *name,
161 				    const char *filename);
162     virtual tape_error_t export_wav(const char *name,
163 				    const char *filename);
164     virtual std::istream * read(const char *name,
165 			   kct_file_props_t *props = NULL);
166 };
167 
168 #endif /* __kc_tape_h */
169