File: reg.cc

package info (click to toggle)
nitpic 0.1-16
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 208 kB
  • ctags: 300
  • sloc: cpp: 2,212; ansic: 408; asm: 144; makefile: 55
file content (221 lines) | stat: -rw-r--r-- 5,519 bytes parent folder | download | duplicates (10)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * reg.cc -- handle events in the registers window
 */
#include "picsim.hh"

void
reg_refresh( Widget w, XEvent *ev, String *prms, Cardinal *nprm )
{
	GC			 gc;
	XGCValues	 xgcv;
	XFontStruct	*xfsp;
	Dimension	 w_height, w_width;
	short		 c_width;
	int			 a, b, i, j, k, dv;
	u_char		 v;
	char		 buf[32];

	XtVaGetValues( W.rdata,
				  XtNforeground, &xgcv.foreground,
				  XtNbackground, &xgcv.background,
				  XtNheight, &w_height,
				  XtNwidth, &w_width,
				  XtNfont, &xfsp,
				  NULL );

	xgcv.font = xfsp->fid;
	
	gc = XCreateGC( XtDisplay(W.rdata), XtWindow(W.rdata),
				   GCForeground | GCBackground | GCFont,
				   &xgcv );

	/*
	 * count the implemented regsters in the page we're looking at
	 */
	k = 0;
	for (i = 0; i < 128; i++) {
		a = Pic.reg_file[Pic.vreg][i].redirect_page;
		b = Pic.reg_file[Pic.vreg][i].redirect_reg;
		if (Pic.reg_file[a][b].implemented) ++k;
	}
	k = (k + 1) >> 1;

	dv = xfsp->ascent + xfsp->descent;
	c_width = xfsp->max_bounds.width;
	
	for (i = j = 0; i < 128; i++) {
		a = Pic.reg_file[Pic.vreg][i].redirect_page;
		b = Pic.reg_file[Pic.vreg][i].redirect_reg;
		if (!Pic.reg_file[a][b].implemented) continue;
		v = Pic.reg_file[a][b].value & Pic.reg_file[a][b].implemented;
		if (c_width * 16 * 2 <= w_width) {
			if (Pic.reg_file[a][b].name)
				sprintf( buf, "%.6s:0x%02X '%c'",
						Pic.reg_file[a][b].name,
						v,
						((v >= ' ' && v <= '~') ? v : '.'));
			else
				sprintf( buf, "   F%02X:0x%02X '%c'",
						i,
						v,
						((v >= ' ' && v <= '~') ? v : '.'));
		} else {
			if (Pic.reg_file[a][b].name)
				sprintf( buf, "%.6s:0x%02X", Pic.reg_file[a][b].name, v );
			else
				sprintf( buf, "   F%02X:0x%02X", i, v );
		}

		if (Pic.reg_file[a][b].modified_last) {
			Pixel	 temp = xgcv.foreground;
			xgcv.foreground = xgcv.background;
			xgcv.background = temp;
			XChangeGC( XtDisplay(W.rdata), gc,
					  GCForeground | GCBackground, &xgcv );
		}

		XDrawImageString( XtDisplay(W.rdata), XtWindow(W.rdata), gc,
						 (j < k) ? 2 : w_width / 2,
						 (j % k + 1) * dv,
						 buf, strlen( buf ) );

		if (Pic.reg_file[a][b].modified_last) {
			Pixel	 temp = xgcv.foreground;
			xgcv.foreground = xgcv.background;
			xgcv.background = temp;
			XChangeGC( XtDisplay(W.rdata), gc,
					  GCForeground | GCBackground, &xgcv );
		}

		++j;
	}

	XFreeGC( XtDisplay(W.rdata), gc );
}

/*
 * Display changed registers in reverse video, and reset all previously-
 * reversed registers to normal video (if they weren't modified again)
 */
void
reg_update( void )
{
	GC			 gc;
	XGCValues	 xgcv;
	XFontStruct	*xfsp;
	Dimension	 w_height, w_width;
	short		 c_width;
	int			 a, b, i, j, k, dv;
	u_char		 v;
	char		 buf[32];

	XtVaGetValues( W.rdata,
				  XtNforeground, &xgcv.foreground,
				  XtNbackground, &xgcv.background,
				  XtNheight, &w_height,
				  XtNwidth, &w_width,
				  XtNfont, &xfsp,
				  NULL );

	xgcv.font = xfsp->fid;
	
	gc = XCreateGC( XtDisplay(W.rdata), XtWindow(W.rdata),
				   GCForeground | GCBackground | GCFont,
				   &xgcv );

	/*
	 * count the implemented regsters in the page we're looking at
	 */
	k = 0;
	for (i = 0; i < 128; i++) {
		a = Pic.reg_file[Pic.vreg][i].redirect_page;
		b = Pic.reg_file[Pic.vreg][i].redirect_reg;
		if (Pic.reg_file[a][b].implemented) ++k;
	}
	k = (k + 1) >> 1;

	dv = xfsp->ascent + xfsp->descent;
	c_width = xfsp->max_bounds.width;
	
	for (i = j = 0; i < 128; i++) {
		a = Pic.reg_file[Pic.vreg][i].redirect_page;
		b = Pic.reg_file[Pic.vreg][i].redirect_reg;
		if (!Pic.reg_file[a][b].implemented) continue;
		if (!(Pic.reg_file[a][b].modified||Pic.reg_file[a][b].modified_last)) {
			++j;
			continue;
		}
		v = Pic.reg_file[a][b].value & Pic.reg_file[a][b].implemented;
		if (c_width * 16 * 2 <= w_width) {
			if (Pic.reg_file[a][b].name)
				sprintf( buf, "%.6s:0x%02X '%c'",
						Pic.reg_file[a][b].name,
						v,
						((v >= ' ' && v <= '~') ? v : '.'));
			else
				sprintf( buf, "   F%02X:0x%02X '%c'",
						i,
						v,
						((v >= ' ' && v <= '~') ? v : '.'));
		} else {
			if (Pic.reg_file[a][b].name)
				sprintf( buf, "%.6s:0x%02X", Pic.reg_file[a][b].name, v );
			else
				sprintf( buf, "   F%02X:0x%02X", i, v );
		}

		if (Pic.reg_file[a][b].modified) {
			Pixel	 temp = xgcv.foreground;
			xgcv.foreground = xgcv.background;
			xgcv.background = temp;
			XChangeGC( XtDisplay(W.rdata), gc,
					  GCForeground | GCBackground, &xgcv );
		}

		XDrawImageString( XtDisplay(W.rdata), XtWindow(W.rdata), gc,
						 (j < k) ? 2 : w_width / 2,
						 (j % k + 1) * dv,
						 buf, strlen( buf ) );

		if (Pic.reg_file[a][b].modified) {
			Pixel	 temp = xgcv.foreground;
			xgcv.foreground = xgcv.background;
			xgcv.background = temp;
			XChangeGC( XtDisplay(W.rdata), gc,
					  GCForeground | GCBackground, &xgcv );
		}

		Pic.reg_file[a][b].modified_last = Pic.reg_file[a][b].modified;
		Pic.reg_file[a][b].modified = 0;
		
		++j;
	}

	XFreeGC( XtDisplay(W.rdata), gc );
}

void
reg_switch( Widget w, XEvent *ev, String *prms, Cardinal *nprm )
{
	char	 buf[32];
	
	if (*nprm == 1) {
		if (!strcmp( *prms, "next" )) {
			Pic.vreg += 1;
			if (Pic.vreg >= Pic.reg_pages) Pic.vreg = 0;
		} else
		if (!strcmp( *prms, "prev" )) {
			Pic.vreg -= 1;
			if (Pic.vreg < 0) Pic.vreg = Pic.reg_pages - 1;
		} else
			goto reg_switch_usage;

		sprintf( buf, "Page %d", Pic.vreg );
		XtVaSetValues( W.rpage, XtNlabel, (XtPointer)buf, NULL );
		reg_refresh( W.rdata, ev, prms, nprm );
		return;
	}

  reg_switch_usage:
	fputs( "usage: reg-switch( [ next | prev ] )\n", stderr );
}