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

 #include "floppy.h"

ISR(TIMER0_OVF_vect)
{
	cli();

	timer_overflow_counter ++;

	*fPORT = 0xff;

	for(uint8_t i = 0; i < 8; i++)
	{
		if(floppy_nextrun[i] == (timer_overflow_counter + 1))
		{
			floppy_nextrun[i] += floppy_frequencies[i];
			floppy_pulse(i);
		}
	}

	sei();
}

void floppy_setup(char *_pulse_port, char *_pulse_ddr, char *_direction_port, char *_direction_ddr)
 {
	//Setup Ports
	fPORT	= _pulse_port;
	fDDR	= _pulse_ddr;
	dPORT	= _direction_port;
	dDDR	= _direction_ddr;

	*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;
	}

	*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] = floppy_nextrun[_floppy_id] = _freq;
 }

 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;
	}
 }