1 /*
2  *  1541d64.h - 1541 emulation in .d64 file
3  *
4  *  Frodo (C) 1994-1997,2002 Christian Bauer
5  */
6 
7 #ifndef _1541D64_H
8 #define _1541D64_H
9 
10 #include "IEC.h"
11 
12 
13 // BAM structure
14 typedef struct {
15 	uint8	dir_track;		// Track...
16 	uint8	dir_sector;		// ...and sector of first directory block
17 	int8	fmt_type;		// Format type
18 	int8	pad0;
19 	uint8	bitmap[4*35];	// Sector allocation
20 	uint8	disk_name[18];	// Disk name
21 	uint8	id[2];			// Disk ID
22 	int8	pad1;
23 	uint8	fmt_char[2];	// Format characters
24 	int8	pad2[4];
25 	int8	pad3[85];
26 } BAM;
27 
28 // Directory entry structure
29 typedef struct {
30 	uint8	type;			// File type
31 	uint8	track;			// Track...
32 	uint8	sector;			// ...and sector of first data block
33 	uint8	name[16];		// File name
34 	uint8	side_track;		// Track...
35 	uint8	side_sector;	// ...and sector of first side sector
36 	uint8	rec_len;		// Record length
37 	int8	pad0[4];
38 	uint8	ovr_track;		// Track...
39 	uint8	ovr_sector;		// ...and sector on overwrite
40 	uint8	num_blocks_l;	// Number of blocks, LSB
41 	uint8	num_blocks_h;	// Number of blocks, MSB
42 	int8	pad1[2];
43 } DirEntry;
44 
45 // Directory block structure
46 typedef struct {
47 	uint8		padding[2];		// Keep DirEntry word-aligned
48 	uint8		next_track;
49 	uint8		next_sector;
50 	DirEntry	entry[8];
51 } Directory;
52 
53 
54 class D64Drive : public Drive {
55 public:
56 	D64Drive(IEC *iec, char *filepath);
57 	virtual ~D64Drive();
58 	virtual uint8 Open(int channel, char *filename);
59 	virtual uint8 Close(int channel);
60 	virtual uint8 Read(int channel, uint8 *byte);
61 	virtual uint8 Write(int channel, uint8 byte, bool eoi);
62 	virtual void Reset(void);
63 
64 private:
65 	void open_close_d64_file(char *d64name);
66 	uint8 open_file(int channel, char *filename);
67 	void convert_filename(char *srcname, char *destname, int *filemode, int *filetype);
68 	bool find_file(char *filename, int *track, int *sector);
69 	uint8 open_file_ts(int channel, int track, int sector);
70 	uint8 open_directory(char *pattern);
71 	uint8 open_direct(int channel, char *filename);
72 	void close_all_channels();
73 	void execute_command(char *command);
74 	void block_read_cmd(char *command);
75 	void buffer_ptr_cmd(char *command);
76 	bool parse_bcmd(char *cmd, int *arg1, int *arg2, int *arg3, int *arg4);
77 	void chd64_cmd(char *d64name);
78 	int alloc_buffer(int want);
79 	void free_buffer(int buf);
80 	bool read_sector(int track, int sector, uint8 *buffer);
81 	int offset_from_ts(int track, int sector);
82 	uint8 conv_from_64(uint8 c, bool map_slash);
83 
84 	char orig_d64_name[256]; // Original path of .d64 file
85 
86 	FILE *the_file;			// File pointer for .d64 file
87 
88 	uint8 *ram;				// 2KB 1541 RAM
89 	BAM *bam;				// Pointer to BAM
90 	Directory dir;			// Buffer for directory blocks
91 
92 	int chan_mode[16];		// Channel mode
93 	int chan_buf_num[16];	// Buffer number of channel (for direct access channels)
94 	uint8 *chan_buf[16];	// Pointer to buffer
95 	uint8 *buf_ptr[16];		// Pointer in buffer
96 	int buf_len[16];		// Remaining bytes in buffer
97 
98 	bool buf_free[4];		// Buffer 0..3 free?
99 
100 	char cmd_buffer[44];	// Buffer for incoming command strings
101 	int cmd_len;			// Length of received command
102 
103 	int image_header;		// Length of .d64 file header
104 
105 	uint8 error_info[683];	// Sector error information (1 byte/sector)
106 };
107 
108 #endif
109