-
Notifications
You must be signed in to change notification settings - Fork 5
/
disk.c
173 lines (137 loc) · 4.63 KB
/
disk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// disk.c
// Steve ][
//
// Created by Tamas Rudnai on 2/15/20.
// Copyright © 2019, 2020 Tamas Rudnai. All rights reserved.
//
// This file is part of Steve ][ -- The Apple ][ Emulator.
//
// Steve ][ is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Steve ][ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Steve ][. If not, see <https://www.gnu.org/licenses/>.
//
#include <stdlib.h>
#include "disk.h"
#include "6502.h"
#include "common.h"
#include "woz.h"
#include "speaker.h"
disk_t disk = {
{ 0, 0, 0 }, // phase
0, // clk_last_access
0, // clk_since_last_read
0, // drive number
};
const int diskAccelerator_frames = 2;
int diskAccelerator_count = 0;
int diskAccelerator_speed = 25; // if less than actual CPU speed means no acceleration
int diskAccelerator_enabled = 0;
int disk_sfx_enabled = 1;
// motor position from the magnet state
// -1 means invalid, not supported
const int magnet_to_Poistion[16] = {
// 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
-1, 0, 2, 1, 4, -1, 3, -1, 6, 7, -1, -1, 5, -1, -1, -1
};
const int position_to_direction[8][8] = {
// N NE E SE S SW W NW
// 0 1 2 3 4 5 6 7
{ 0, 1, 2, 3, 0, -3, -2, -1 }, // 0 N
{ -1, 0, 1, 2, 3, 0, -3, -2 }, // 1 NE
{ -2, -1, 0, 1, 2, 3, 0, -3 }, // 2 E
{ -3, -2, -1, 0, 1, 2, 3, 0 }, // 3 SE
{ 0, -3, -2, -1, 0, 1, 2, 3 }, // 4 S
{ 3, 0, -3, -2, -1, 0, 1, 2 }, // 5 SW
{ 2, 3, 0, -3, -2, -1, 0, 1 }, // 6 W
{ 1, 2, 3, 0, -3, -2, -1, 0 }, // 7 NW
};
const uint8_t phy2log_dos33[16] = {
0, 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8, 15
};
const uint8_t log2phy_dos33[16] = {
0, 13, 11, 9, 7, 5, 3, 1, 14, 12, 10, 8, 6, 4, 2, 15
};
const uint8_t phy2log_pascal[16] = {
0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15
};
const uint8_t log2phy_pascal[16] = {
0, 2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13, 15
};
const uint8_t phy2log_cpm[16] = {
0, 11, 6, 1, 12, 7, 2, 13, 8, 3, 14, 9, 4, 15, 10, 5
};
const uint8_t log2phy_cpm[16] = {
0, 3, 6, 9, 12, 15, 2, 5, 8, 11, 14, 1, 4, 7, 10, 13
};
void disk_accelerator_speedup(void) {
if ( ( diskAccelerator_enabled ) && ( FRAME(diskAccelerator_speed) >= clk_6502_per_frm ) ) {
clk_6502_per_frm =
clk_6502_per_frm_max = FRAME(diskAccelerator_speed); // clk_6502_per_frm_diskAccelerator;
diskAccelerator_count = diskAccelerator_frames;
}
}
void disk_phase(void) {
int position = magnet_to_Poistion[disk.phase.magnet];
if ( position >= 0 ) {
int lastPosition = disk.phase.count & 7;
int direction = position_to_direction[lastPosition][position];
disk.phase.count += direction;
if( disk.phase.count < minDiskPhaseNum ) {
disk.phase.count = minDiskPhaseNum;
spkr_play_disk_ioerr();
}
else
if( disk.phase.count > maxDiskPhaseNum ) {
disk.phase.count = maxDiskPhaseNum;
spkr_play_disk_ioerr();
}
else {
spkr_play_disk_arm();
}
// TODO: Add track positioning sfx
// spkr_toggle();
// printf("Head Position: p:%d d:%d l:%d: ph:%u)\n", position, direction, lastPosition, disk.phase.count);
disk.clk_last_access = m6502.clktime;
disk_accelerator_speedup();
}
else {
// invalid magnet config
}
// printf("\n");
}
void disk_phase_on( uint8_t currentMagnet ) {
disk.phase.magnet |= 1 << currentMagnet;
disk_phase();
}
void disk_phase_off( uint8_t currentMagnet ) {
disk.phase.magnet &= ~(1 << currentMagnet);
disk_phase();
}
void disk_motor_on(void) {
spkr_play_disk_motor();
spkr_stop_disk_motor( -1 );
}
void disk_motor_off(void) {
spkr_stop_disk_motor( 3 * fps ); // 3 second delay
}
uint8_t disk_read(void) {
dbgPrintf("io_DISK_READ (S%u)\n", 6);
disk.clk_last_access = m6502.clktime;
disk_accelerator_speedup();
// Debug disk read
// spkr_toggle();
if ( disk.drive ) {
return rand();
}
return woz_read();
}