summaryrefslogtreecommitdiff
path: root/emu/7seg.c
blob: f4bc25e5360d3b7216f4dacd16233889f09fc45c (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
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
#include "7seg.h"

#include <stdio.h>
#include <stdbool.h>

#ifdef WITH_GUI

#endif

static device_vtable_t const seg7_vtable = {
	seg7_read,
	seg7_write,
#ifdef WITH_GUI
	seg7_draw,
#endif
	seg7_deinit
};

void seg7_init( seg7_t *seg, uint16_t addr, bool initialize )
{
	device_init( &seg->base, "seg7" );
	seg->base.vtable = (device_vtable_t *)&seg7_vtable;

	seg->addr = addr;
	seg->debug = false;
	
	if( initialize ) {
		seg->latch = 0x0000;
		seg->shift = 0x0000;
	}
}

uint8_t seg7_read( void *obj, uint16_t addr )
{
	return 0;
}

void seg7_write( void *obj, uint16_t addr, uint8_t data )
{
	seg7_t *seg = (seg7_t *)obj;
	
	switch( addr - seg->addr ) {
		case DDRA:
			seg->ddr = data;
			break;
		
		case PORTA:
			// write bits from SER into shift register on positive SRCLK
			if( ( seg->ddr | ( SRCLK & SER ) ) && ( data & SRCLK ) ) {
				seg->shift = ( seg->shift << 1 ) | ( data & SER );
				if( seg->debug ) {
					fprintf( stderr, "7seg shifting data, data is now %02X\n", seg->shift );
				}
			}
			
			// copy shift to latch register on positive RCLK
			if( ( seg->ddr | RCLK ) && ( data & RCLK ) ) {
				seg->latch = seg->shift;
				if( seg->debug ) {
					fprintf( stderr, "7seg copying %02X from shift to latch\n", seg->latch );
				}
			}
			
			break;
		
		// ignore writes to other addresses
	}
}

#ifdef WITH_GUI

enum {
	NOF_DIGITS = 16,
	NOF_SEGS = 7,
	// direction of the segment (vertical or horizontal)
	H = 0,
	V = 1,
	// segment positions in the segs array (bit)
	A = 6,
	B = 5,
	C = 4,
	D = 3,
	E = 2,
	F = 1,
	G = 0
};

static int segs[NOF_DIGITS] = {
	0x7E, 0x30, 0x6D, 0x79, 0x33,
	0x5B, 0x5F, 0x70, 0x7F, 0x7B,
	0x77, 0x1F, 0x4E, 0x3D, 0x4F,
	0x47
};

static int x_pos[NOF_SEGS] = {
	5, 45, 45,  5,  5,  5 , 5 
};

static int y_pos[NOF_SEGS] = 
{
	5,  5, 45, 90, 45,  5, 45
};

static int direction[NOF_SEGS] =
{
	H, V, V, H, V, V, H
};

static void printSegment( SDL_Renderer *renderer, int x, int y, int direction, bool enabled )
{
	int r = 80;
	
	if( enabled ) {
		r = 255;
	}
			
	switch( direction ) {
		case H:
			boxRGBA( renderer, x-3, y-3, x+40, y+3, r, 0x00, 0x00, 0xFF );	
			break;
		
		case V:
			boxRGBA( renderer, x-3, y-3, x+3, y+40, r, 0x00, 0x00, 0xFF );	
			break;
	}
}

static void printDigit( SDL_Renderer *renderer, int x, int y, int digit )
{
	int v = ( 1 << 6 );
	for( int i = 0; i < 7; i++ ) {
		printSegment( renderer, x + x_pos[i], y + y_pos[i], direction[i], v & segs[digit] );
		v >>= 1;
	}
}	

void seg7_draw( void *obj, SDL_Renderer *renderer )
{
	seg7_t *seg = (seg7_t *)obj;
	
	boxRGBA( renderer, 10, 10, 110, 110, 0x00, 0x00, 0x00, 0xFF );
	
	printDigit( renderer, 10, 10, ( seg->latch & 0x00F0 ) >> 4 );
	printDigit( renderer, 60, 10, seg->latch & 0x000F );
}
#endif

void seg7_deinit( void *obj )
{
	seg7_t *seg = (seg7_t *)obj;

	device_deinit( &seg->base );
}