summaryrefslogtreecommitdiffstats
path: root/Graphic_Equalizer/src/smartmedia.hcc
blob: 2671b8a78b88bf6496082de50493de61ab5ba761 (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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*! \file smartmedia.hcc
 *
 * \section generic	Here we interface with the SmartMedia card.
 *
 * \section project Project information.
 * Project Graphic Equalizer\n
 * \author O.M. Schinagl
 * \date 20041110
 * \version 0.1
 *
 * \section copyright Copyright
 * Copyright 2004 Koninklijke Philips Electronics N.V. All rights reserved
 *
 * \section history Change history
 * 20041110: O.M. Schinagl\n	Initial version
 *
 ********************************************************************/

/******** System Includes *************/
#include <stdlib.hch>

#include "pal_master.hch"

/******** Application Includes ********/
#include "configuration.hch"
#include "display_shared.hch"
#include "smartmedia_shared.hch"
#include "smartmedia.hch"

#if HAVE_DEBUG
	#include "debug.hch"
#endif

#include "audio.hch"
#include "mouse_shared.hch"
#include "eventhandler_shared.hch"
#include "display.hch" /* FIXME: temporary include, needs to be moved to 'init' */


unsigned 1 physical_format;
unsigned 2 current_track;

static rom unsigned 27 track_start[4] = {
	SMARTMEDIA_ADDRESS_TRACK1_START,
	SMARTMEDIA_ADDRESS_TRACK2_START,
	SMARTMEDIA_ADDRESS_TRACK3_START
};

static rom unsigned 27 track_end[4] = {
	SMARTMEDIA_ADDRESS_TRACK1_END,
	SMARTMEDIA_ADDRESS_TRACK2_END,
	SMARTMEDIA_ADDRESS_TRACK3_END
};

/*! \fn		unsigned 1 smartmedia_init(void);
 * \brief	We here initialize the Smart Media card and verify wether the
 *		card is inserted and such.
 * 
 * \return	We return 0 on success, 1 on error.
 * \retval	unsigned 1
 */
inline unsigned 1 smartmedia_init(void) {
	unsigned 1 retval;
	/*
	 * Firstly we enable both the CPLD.
	 */
	RC200CPLDEnable();

	/*
	 * We must reset the Smart Media before initializing it. If we don't reset it
	 * we get a lot of Init failures. If we don't use the delay, we get a lot of
	 * init failures.
	 */
	RC200SmartMediaReset(&retval);
	delay;
	RC200SmartMediaInit(&retval);

	
	/*
	 * Before we enter our main Smart Media read loop we verify the format
	 * of the SMC. This to ensure we use the correct functions later.
	 */
	RC200SmartMediaCheckLogicalFormat(&physical_format);

	current_track = 0;
	return retval;	
} /* --- smartmedia_init() --- */



/*! \fn		void smartmedia_loaddata(skindata_t *skindata);
 * \brief	We load our memory with skin and help data from the smartmedia.
 * 
 * \param	*skindata	skindata like boundries and colors of elements.
 *
 * \return	void
 * \retval	void
 */
void smartmedia_loaddata(skindata_t *skindata) {
	/*
	 * Setup RAM Handle, and determin maximum Data and Address widths
	 *
	 * FIXME: Currently we set the Ram handle here and in the main. If we
	 * would want to change it here, also change it elsewhere. A better
	 * solution would be to have the RAM_BANK0 macro defined somewhere
	 * globally.
	 */
	macro expr RAM_BANK0 = PalPL2RAMCT(0);
	macro expr DW = PalPL2RAMGetMaxDataWidthCT();
	macro expr AW = PalPL2RAMGetMaxAddressWidthCT();

	unsigned DW data;
	unsigned 27 sm_address;
	unsigned AW address, address_end;
	unsigned 8 mask, r, g, b;
	unsigned 4 stage;
	unsigned 1 result;

	extern ram unsigned 8 presets_default_values[768];	

	/*
	 * We have several stages to go through. We stop once we pass the last
	 * one.
	 */
	while ((STAGE_LOAD_INPUT_SELECT_TEXT +1) != stage) {
		/*
		 * For each iteration of the main loop we set a different
		 * start and end variables.
		 */
		switch (stage) {
			case STAGE_LOAD_DEMO_PRESET:
				sm_address = SMARTMEDIA_ADDRESS_PRESET_DEMO_START;
				address = INDEX_PRESET_DEMO_START;
				address_end = INDEX_PRESET_DEMO_END;
				break;
			case STAGE_LOAD_RESET_PRESET:
				sm_address = SMARTMEDIA_ADDRESS_PRESET_RESET_START;
				address = INDEX_PRESET_RESET_START;
				address_end = INDEX_PRESET_RESET_END;
				break;
			case STAGE_LOAD_SKIN:
				sm_address = SMARTMEDIA_ADDRESS_SKIN_START;
				address = ADDRESS_SKIN_START;
				address_end = ADDRESS_SKIN_END;
				break;
			case STAGE_LOAD_HELP:
				sm_address = SMARTMEDIA_ADDRESS_HELP_START;
				address = ADDRESS_HELP_START;
				address_end = ADDRESS_HELP_END;
				break;
			case STAGE_LOAD_GRAPH:
				sm_address = SMARTMEDIA_ADDRESS_GRAPH_START;
				address = ADDRESS_GRAPH_START;
				address_end = ADDRESS_GRAPH_END;
				break;
			case STAGE_LOAD_TOP_FONTYS:
				sm_address = SMARTMEDIA_ADDRESS_TOP_FONTYS_START;
				address = ADDRESS_ABOUT_TOP_FONTYS_START;
				address_end = ADDRESS_ABOUT_TOP_FONTYS_END;
				break;
			case STAGE_LOAD_TOP_TASS:
				sm_address = SMARTMEDIA_ADDRESS_TOP_TASS_START;
				address = ADDRESS_ABOUT_TOP_TASS_START;
				address_end = ADDRESS_ABOUT_TOP_TASS_END;
				break;
			case STAGE_LOAD_TOP_TRANSFER:
				sm_address = SMARTMEDIA_ADDRESS_TOP_TRANSFER_START;
				address = ADDRESS_ABOUT_TOP_TRANSFER_START;
				address_end = ADDRESS_ABOUT_TOP_TRANSFER_END;
				break;
			case STAGE_LOAD_TOP_CELOXICA:
				sm_address = SMARTMEDIA_ADDRESS_TOP_CELOXICA_START;
				address = ADDRESS_ABOUT_TOP_CELOXICA_START;
				address_end = ADDRESS_ABOUT_TOP_CELOXICA_END;
				break;
/*
			case STAGE_LOAD_TOP_DETAILS:
				sm_address = SMARTMEDIA_ADDRESS_TOP_DETAILS_START;
				address = ADDRESS_ABOUT_TOP_DETAILS_START;
				address_end = ADDRESS_ABOUT_TOP_DETAILS_END;
				break;
*/
			case STAGE_LOAD_ABOUT_BOTTOM:
				sm_address = SMARTMEDIA_ADDRESS_BOTTOM_START;
				address = ADDRESS_ABOUT_BOTTOM_START;
				address_end = ADDRESS_ABOUT_BOTTOM_END;
				break;

			case STAGE_LOAD_AUDIO_PLAYER:
				sm_address = SMARTMEDIA_ADDRESS_AUDIO_PLAYER_START;
				address = ADDRESS_AUDIO_PLAYER_START;
				address_end = ADDRESS_AUDIO_PLAYER_END;
				break;
			case STAGE_LOAD_INPUT_SELECT_TEXT:
				sm_address = SMARTMEDIA_ADDRESS_INPUT_SELECT_TEXT_START;
				address = ADDRESS_INPUT_SELECT_TEXT_START;
				address_end = ADDRESS_INPUT_SELECT_TEXT_END;
				break;
			default:
				break;
		}
		/*
		 * TODO: Replace the above switch-case with a lookuptables.
		 */

		/*
		 * The only difference between logical and phyiscally formated
		 * SMCs codewise is how they are addressed.
		 */
		if (physical_format) {
			RC200SmartMediaSetAddress(READ, sm_address);
		} else {
			RC200SmartMediaSetLogicalAddress(READ, sm_address);
		}
		
		/*
		 * While the address hasn't reached the end, continue loading data.
		 */
		while (address != address_end) {

			/*
			 * No matter what we read from the SMC, we aways need to
			 * read at least one byte per iteration. (Otherwise there
			 * would be no point in entering this loop.
			 */
			RC200SmartMediaRead(&mask, FALSE);

			/*
			 * The difference when loading data from the SMC is
			 * when we load the presets. These aren't stored in
			 * external RAM but in an array on the FPGA.
			 */
			if ((STAGE_LOAD_DEMO_PRESET == stage) || (STAGE_LOAD_RESET_PRESET == stage)) {
				/*
				 * We only read one byte from the SMC and thus
				 * we only store one byte. We re-use the 'mask'
				 * variable here. The address is also re-used
				 * and thus we only use the last 10 bits to fit
				 * the address in our index.
				 *
				 */
				presets_default_values[address <- 10] = mask;
			} else {
				/*
				 * All other data has RGB image data and thus
				 * we read those additional bytes from the SMC.
				 * The image used for the graphic visualization
				 * is packed together however and thus we
				 * re-use the rgb variables here.
				 */
				RC200SmartMediaRead(&r, FALSE);
				RC200SmartMediaRead(&g, FALSE);
				RC200SmartMediaRead(&b, FALSE);

				/*
				 * There needs to be atleast one clock cycle
				 * between setting the address and reading
				 * from it. We therefore set the address before
				 * reading from the SmartMedia as 'delay'.
				 */
				PalPL2RAMSetWriteAddress(RAM_BANK0, address);

				/*
				 * FIXME: Do we need this even?
				 */
				data = 0 @ mask @ r @ g @ b;

				/*
				 * Now that we read a while 32bit wide word
				 * we can store it in the main memory bank.
				 */
				PalPL2RAMWrite(RAM_BANK0, data);
			}

#if HAVE_DEBUG
			/*
			 * Print some indication about data loading.
			 */
			if (!(address <- 14)) {;
				print_string(".");
			}
#endif
			/*
			 * Finally we increase our address to prepare for the
			 * next iteration.
			 */
			address++;
		}
		/*
		 * We need to tell the SmartMedia that the last byte was read
		 * however to check wether it is the last byte during each
		 * iteration of the loop would unecasserly 'a lot' of
		 * resources. We therefore read a dummy byte here. It doesn't
		 * matter where it comes from (from the SMC) as we don't use
		 * it.
		 */
		RC200SmartMediaRead(&mask, TRUE);
		/*
		 * Because we need to set a new starting address in the next
		 * iteration we need to 'end' the SmartMedia Operation.
		 */
		RC200SmartMediaOperationEnd(&result);

		/*
		 * We are done with this 'stage' and continue to the next one.
		 */
		stage++;
	}
	if (physical_format) 
	{
		RC200SmartMediaSetAddress(READ, 0);
	} else {
		RC200SmartMediaSetLogicalAddress(READ, 0);
	}
	/*
	 * TODO: This block needs to move up into the loop where we calculate
	 * these settings determind by the image data.
	 */
	skindata->spectrum.top = 200;
	skindata->spectrum.bottom = 335;
	skindata->spectrum.left = 77;
	skindata->spectrum.right = 575;
//	skindata->spectrum.color_primary = PIXEL_SPECTRUM;
//	skindata->spectrum.color_secondary = PIXEL_SPECTRUM;

	skindata->waveform.top = 46;
	skindata->waveform.bottom = 118;
	skindata->waveform.left = 76;
	skindata->waveform.right = 413;
	skindata->waveform.color_primary = PIXEL_WAVEFORM;

	skindata->volume.top = 66;
	skindata->volume.bottom = 112;
	skindata->volume.left = 431;
	skindata->volume.right = 448;
	skindata->volume.color_primary = PIXEL_VOLUME;

	skindata->inputgain.top = 66;
	skindata->inputgain.bottom = 112;
	skindata->inputgain.left = 450;
	skindata->inputgain.right = 467;
	skindata->inputgain.color_primary = PIXEL_INPUTGAIN_NORM;
	skindata->inputgain.color_secondary = PIXEL_INPUTGAIN_SAT;

	skindata->equalizer.color_primary = PIXEL_EQUALIZER;
} /* --- smartmedia_loaddata() --- */

/*
smartmedia_load_block() {
	
	setaddress();
	for () {
		smart_read();
	}
	end();
}*/

void change_track(unsigned 2 track_index)
{
	current_track = track_index;
}

unsigned 1 load_audio_samples(signed 18 *samples, unsigned 27 blockoffset, unsigned 7 sample_count) {
	ram unsigned 8 data[128];
	
	unsigned 16 sampleword;
	unsigned 8 sampleindex, samplebyte, samplecount;
	unsigned 1 result;
	unsigned 1 retval;
	
	samplecount = (0 @ sample_count) <<1;
	sampleindex = 0;
	retval = 0;

	if (!blockoffset) {
		smartmedia_set_read_address(track_start[current_track]);
	}
	
	smartmedia_read_bytes(data, samplecount);

	while (sampleindex != samplecount) {
		samplebyte = data[sampleindex <- 7];
		sampleword = samplebyte @ data[(sampleindex +1) <- 7];
		samples[sampleindex >>1] = (signed 18)(sampleword @ 0);
		sampleindex +=2;
	}

	if (((blockoffset <<7) +(0 @ samplecount) +track_start[current_track]) >= track_end[current_track]) {
		retval = 1;
	}
	return retval;
}

void smartmedia_set_read_address(unsigned 27 address)
{
	unsigned 8 dummy;
	unsigned 1 result;
	
	/* 
	 * Read dummy value to signal the SmartMedia card reader 
	 * that the last byte is read from the SmartMedia card
   	 */
	RC200SmartMediaRead(&dummy, TRUE);
	RC200SmartMediaOperationEnd(&result);
	/* 
	 * Check the format of the SmartMedia card and set the address accordingly 
   	 */
	if (physical_format) 
	{
		RC200SmartMediaSetAddress(READ, address);
	} else {
		RC200SmartMediaSetLogicalAddress(READ, address);
	}
}

void smartmedia_read_bytes(unsigned 8 *data, unsigned 8 bytecount) 
{
	unsigned 8 byteindex;
	unsigned 8 temp;	

	byteindex = 0;
	while (byteindex != bytecount) {
		RC200SmartMediaRead(&temp, FALSE);
		data[byteindex] = temp;
		byteindex++;
	}
}