summaryrefslogtreecommitdiff
path: root/floppyMusic/floppy.c
blob: b065302a59154feaf4f131f3b858e2040042d0af (plain)
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
/*
 * floppy.c
 *
 * Created: 29.04.2017 17:06:37
 *  Author: Jonas
 */ 

 #include "floppy.h"

ISR(TIMER0_OVF_vect)
{
	cli();

	timer_overflow_counter ++;

	*fPORT = 0xff; //Deactivate all previously activated pins

	for(uint8_t i = 0; i < 8; i++) //Perform check of every channel
	{
		if(floppy_nextrun[i] == timer_overflow_counter && floppy_frequencies[i] > 0) //check if pulse is due
		{
			floppy_nextrun[i] += floppy_frequencies[i]; //set next pulse
			floppy_pulse(i);
		}
	}

	sei();
}

void floppy_setup(unsigned char *_pulse_port, unsigned char *_pulse_ddr, unsigned char *_direction_port, unsigned char *_direction_ddr)
 {
	//Setup Ports
	fPORT	= _pulse_port;
	fDDR	= _pulse_ddr;
	dPORT	= _direction_port;
	dDDR	= _direction_ddr;
	
	//Floppy inputs are active low so they are initialized high
	*fDDR	= 0xff;
	*fPORT	= 0xff;
	*dDDR	= 0xff;
	*dPORT	= 0xff;
	
	//Variable init
	timer_overflow_counter = 0;

	for(uint8_t i = 0; i < 8; i++)
	{
		floppy_head_return_counter[i] = 0;
		floppy_frequencies[i] = 0;
		floppy_nextrun[i] = 0;
	}

	//Return all FDDs to track 0
	for(uint8_t i = 0; i < 200; i++)
	{
		_delay_ms(5);
		*fPORT ^= 0xff;
	}

	*dPORT	= 0x00; //Head needs to be reversed
	*fPORT	= 0xff;

	//Setup Timer
	TCCR0	|= (1 << CS01); // Prescaler 8 To leave enough time for ISR to complete
	TIMSK	|= (1 << TOIE0); //Activate Interrupt

	sei();
 }

 void floppy_set_frequency(uint8_t _floppy_id, uint8_t _freq)
 {
	floppy_frequencies[_floppy_id] = _freq;
	floppy_nextrun[_floppy_id] = _freq + timer_overflow_counter;
 }

 void floppy_pulse(uint8_t _floppy_id)
 {
	*fPORT &= ~(1 << _floppy_id); //Activate pin

	floppy_head_return_counter[_floppy_id] ++;

	if(floppy_head_return_counter[_floppy_id] > 75) //Toggle direction pin if more than 75 steps performed
	{
		*dPORT ^= (1<<_floppy_id);
		floppy_head_return_counter[_floppy_id] = 0;
	}
 }

 uint8_t floppy_calc_freq(uint32_t _f_hz)
 {

	///TODO Shift notes out of effective range of FDD into range
	if(_f_hz < 31)
		return 255;
	if(_f_hz > 7813)
		return 1;

	return 7913 / _f_hz;
 }