digitled.c 15.8 KB
Newer Older
饶先宏's avatar
饶先宏 已提交
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
/*
** HDL4SE: 软件Verilog综合仿真平台
** Copyright (C) 2021-2021, raoxianhong<raoxianhong@163.net>
** LCOM: 轻量级组件对象模型
** Copyright (C) 2021-2021, raoxianhong<raoxianhong@163.net>
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
**
** * Redistributions of source code must retain the above copyright notice,
**   this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright notice,
**   this list of conditions and the following disclaimer in the documentation
**   and/or other materials provided with the distribution.
** * The name of the author may be used to endorse or promote products
**   derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
** THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
* digitled.c
    202105221536: rxh, initial version
*/

#include "stdlib.h" 
#include "stdio.h"
#include "object.h"
#include "dlist.h"
#include "bignumber.h"
#include "hdl4secell.h"

#include "glad/gl.h"
#include "GLFW/glfw3.h"

#define IMPLEMENT_GUID
#include "digitled.h"
#undef IMPLEMENT_GUID

#define WIDTH  800
#define HEIGHT 300

#define LEDCOUNT 10

/*
digit led & keyboard
*/
typedef struct _sDigitLed {
	OBJECT_HEADER
	INTERFACE_DECLARE(IHDL4SEUnit)
	HDL4SEUNIT_VARDECLARE
	DLIST_VARDECLARE
		
	IHDL4SEModule** parent;
	char* name;
	GLFWwindow* window;
	int width;
	int height;
饶先宏's avatar
饶先宏 已提交
70
	int count;
饶先宏's avatar
饶先宏 已提交
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
	unsigned int baseaddr;


	unsigned char ledvalue[LEDCOUNT];
	float ledcolor[LEDCOUNT][8][3];
	
	unsigned int keypressed;

	int wRead;              /* 上周期的读信号 */
	unsigned int bReadAddr; /* 决定当前周期是否响应 */
	int wRead_cur;
	unsigned int bReadAddr_cur;

	/* 
	从设备的7个输入端口 
		0.nwReset
		1.wWrite
		2.bWriteAddr
		3.bWriteData
		4.bWriteMask
		5.wRead
		6.bReadAddr
		7.bReadData
	*/
	IHDL4SEUnit** fromunit[7];
	int           fromindex[7];

}sDigitLed;

OBJECT_FUNCDECLARE(digitled, CLSID_DIGITLED);
HDL4SEUNIT_FUNCDECLARE(digitled, CLSID_DIGITLED, sDigitLed);
DLIST_FUNCIMPL(digitled, CLSID_DIGITLED, sDigitLed);

OBJECT_FUNCIMPL(digitled, sDigitLed, CLSID_DIGITLED);


QUERYINTERFACE_BEGIN(digitled, CLSID_DIGITLED)
QUERYINTERFACE_ITEM(IID_HDL4SEUNIT, IHDL4SEUnit, sDigitLed)
QUERYINTERFACE_ITEM(IID_DLIST, IDList, sDigitLed)
QUERYINTERFACE_END

static const char* digitledModuleInfo()
{
	return "1.0.0-20210522.1050 Digit Led";
}

static int keypressed = 0;

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
	if (action != GLFW_PRESS)
		return;
	if (key >= GLFW_KEY_F1 && key <= GLFW_KEY_F10) {
		key -= GLFW_KEY_F1;
		if (keypressed & (1 << key))
			keypressed &= ~(1 << key);
		else
			keypressed |= (1 << key);
	}
}

static int digitledCreate(const PARAMITEM* pParams, int paramcount, HOBJECT* pObject)
{
	sDigitLed* pobj;
	int i;
	pobj = (sDigitLed*)malloc(sizeof(sDigitLed));
	if (pobj == NULL)
		return -1;
	*pObject = 0;
	HDL4SEUNIT_VARINIT(pobj, CLSID_DIGITLED);
	INTERFACE_INIT(IHDL4SEUnit, pobj, digitled, hdl4se_unit);
	DLIST_VARINIT(pobj, digitled);

	pobj->baseaddr = 0xF0000000;
	pobj->name = NULL;
	pobj->parent = NULL;
	for (i = 0; i < LEDCOUNT; i++) {
		int j;
		for (j = 0; j < 8; j++) {
			pobj->ledcolor[i][j][0] = 0.8f;
			pobj->ledcolor[i][j][1] = 0.1f;
			pobj->ledcolor[i][j][2] = 0.1f;
		}
		pobj->ledvalue[i] = i*23;
	}
	for (i = 0; i < paramcount; i++) {
		if (pParams[i].name == PARAMID_HDL4SE_UNIT_NAME) {
			if (pobj->name != NULL)
				free(pobj->name);
			pobj->name = strdup(pParams[i].pvalue);
		}
		else if (pParams[i].name == PARAMID_BASEADDR) {
			pobj->baseaddr = pParams[i].i32value;
		}
	}

	for (i = 0; i < 7; i++) {
		pobj->fromunit[i] = NULL;
	}

	if (!glfwInit())
		return -1;
	pobj->width = WIDTH;
	pobj->height = HEIGHT;
	pobj->wRead = 0;
饶先宏's avatar
饶先宏 已提交
176
	pobj->count = 0;
饶先宏's avatar
饶先宏 已提交
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
	pobj->wRead_cur = 0;
	pobj->window = glfwCreateWindow(WIDTH, HEIGHT, "DIGIT LED", NULL, NULL);
	if (!pobj->window)
	{
		glfwTerminate();
		return -2;
	}
	pobj->keypressed = 0;

	glfwSetWindowAspectRatio(pobj->window, 3, 1);
	
	glfwSetKeyCallback(pobj->window, key_callback);
	glfwMakeContextCurrent(pobj->window);
	gladLoadGL(glfwGetProcAddress);
	glfwSwapInterval(1);

	glfwGetFramebufferSize(pobj->window, &pobj->width, &pobj->height);

	/* 返回生成的对象 */
	OBJECT_RETURN_GEN(digitled, pobj, pObject, CLSID_DIGITLED);
	return EIID_OK;
}

static void digitledDestroy(HOBJECT object)
{
	sDigitLed* pobj;
	int i;
	pobj = (sDigitLed*)objectThis(object);
	if (pobj->name != NULL)
		free(pobj->name);
	for (i = 0; i < 7; i++) {
		objectRelease(pobj->fromunit[i]);
	}
	glfwTerminate();
	memset(pobj, 0, sizeof(sDigitLed));
	free(pobj);
}

static int digitledValid(HOBJECT object)
{
	sDigitLed* pobj;
	pobj = (sDigitLed*)objectThis(object);
	return 1;
}

static int digitled_hdl4se_unit_Connect(HOBJECT object, int index, HOBJECT from, int fromindex)
{
	sDigitLed* pobj;
	IHDL4SEUnit** unit = NULL;
	pobj = (sDigitLed*)objectThis(object);
	if (index >= 0 && index < 7) {
		if (0 == objectQueryInterface(from, IID_HDL4SEUNIT, (void**)&unit)) {
			pobj->fromunit[index] = unit;
			pobj->fromindex[index] = fromindex;
			return 0;
		}
		else {
			return -2;
		}
	}
	return -1;
}

#define isLedAddr(addr) ((addr & 0xFFFFFFE0) == (pobj->baseaddr & 0xFFFFFFE0))

static int digitled_hdl4se_unit_GetValue(HOBJECT object, int index, int width, IBigNumber** value)
{
	int i;
	int sel;
	sDigitLed* pobj;
	pobj = (sDigitLed*)objectThis(object);
	if (index != 7) /* 只响应7.ReadData端口 */
		return -1;
	if (pobj->wRead == 0)
		return -2; /* 上周期没有读命令,不响应,高阻状态 */
	if (pobj->bReadAddr == 0) {
		/* 偏移地址为0,读按键状态 */
		objectCall1(value, AssignInt, pobj->keypressed);
		return 0;
	}
	return -2;
}

static int digitled_hdl4se_unit_ClkTick(HOBJECT object)
{
	int i, j;
	int reset;
	sDigitLed* pobj;
	IBigNumber** temp = bigintegerCreate(32);
	pobj = (sDigitLed*)objectThis(object);
	pobj->keypressed = keypressed;
	reset = 1;
	/* 读nwReset信号,看是否复位 */
	if (0 == objectCall3(pobj->fromunit[0], GetValue, 0, 32, temp)) {
		objectCall1(temp, GetInt, &reset);
		if (reset == 0) {
			for (i = 0; i < LEDCOUNT; i++) {
				pobj->ledvalue[i] = 0;
			}
			pobj->keypressed = 0;
		}
	}

	/* 读写命令,看是否写LED状态 */
	if (reset != 0) {
		int wWrite = 0;
		unsigned int bWriteAddr = 0xFFFFFFFF;
		int wRead = 0;
		unsigned int bReadAddr = 0xFFFFFFFF;
		objectCall1(temp, AssignInt, 0);
		if (0 == objectCall3(pobj->fromunit[1], GetValue, 1, 32, temp)) {
			objectCall1(temp, GetInt, &wWrite);
		}
		objectCall1(temp, AssignInt, 0);
		objectCall2(temp, SetWidth, 32, 0);
		if (wWrite && (0 == objectCall3(pobj->fromunit[2], GetValue, 2, 32, temp))) {
			objectCall1(temp, GetInt, &bWriteAddr);
		}
		if (isLedAddr(bWriteAddr)) {
			bWriteAddr &= 0x1f;
			if (bWriteAddr >= 0x10 && bWriteAddr <= 0x18) {
				objectCall1(temp, AssignInt, 0);
				objectCall2(temp, SetWidth, 32, 0);
				if (0 == objectCall3(pobj->fromunit[3], GetValue, 3, 32, temp)) {
					unsigned int bWriteData;
					objectCall1(temp, GetInt, &bWriteData);
					if (bWriteAddr == 0x10) {
						pobj->ledvalue[0] = bWriteData & 0xFF;
						pobj->ledvalue[1] = (bWriteData >> 8) & 0xFF;
						pobj->ledvalue[2] = (bWriteData >> 16) & 0xFF;
						pobj->ledvalue[3] = (bWriteData >> 24) & 0xFF;
					} 
					else if (bWriteAddr == 0x14) {
						pobj->ledvalue[4] = bWriteData & 0xFF;
						pobj->ledvalue[5] = (bWriteData >> 8) & 0xFF;
						pobj->ledvalue[6] = (bWriteData >> 16) & 0xFF;
						pobj->ledvalue[7] = (bWriteData >> 24) & 0xFF;
					}
					else if (bWriteAddr == 0x18) {
						pobj->ledvalue[8] = bWriteData & 0xFF;
						pobj->ledvalue[9] = (bWriteData >> 8) & 0xFF;
					}
				}
			}
		}
		objectCall1(temp, AssignInt, 0);
		objectCall2(temp, SetWidth, 32, 0);
		if (0 == objectCall3(pobj->fromunit[5], GetValue, 5, 32, temp)) {
			objectCall1(temp, GetInt, &wRead);
		}
		pobj->wRead_cur = 0;
		objectCall1(temp, AssignInt, 0);
		objectCall2(temp, SetWidth, 32, 0);
		if (wRead && (0 == objectCall3(pobj->fromunit[6], GetValue, 6, 32, temp))) {
			objectCall1(temp, GetInt, &bReadAddr);
		}
		if (isLedAddr(bReadAddr)) {
			bReadAddr &= 0x1f;
			if (bReadAddr == 0x0) {
				pobj->wRead_cur = 1;
				pobj->bReadAddr_cur = 0;
			}
		}
	}
	objectRelease(temp);

	for (i = 0; i < LEDCOUNT; i++) {
		for (j = 0; j < 8; j++) {
			if (pobj->ledvalue[i] & (1 << j)) {
				pobj->ledcolor[i][j][0] = 1.0f;
				pobj->ledcolor[i][j][1] = 0.1f; 
				pobj->ledcolor[i][j][2] = 0.1f;
			}
			else {
				pobj->ledcolor[i][j][0] *= 0.95f;
				pobj->ledcolor[i][j][1] *= 0.95f;
				pobj->ledcolor[i][j][2] *= 0.95f;
				if (pobj->ledcolor[i][j][0] < 0.2f)
					pobj->ledcolor[i][j][0] = 0.2f;
			}
		}
	}

	return 0;
}

#define LEDWIDTH  0.11f
#define LEDHEIGHT 0.80f
#define LEDGAPX    0.01f
#define LEDTHINX   0.015f
#define LEDANGLEX  0.02f

#define LEDGAPY    0.02f
#define LEDTHINY   0.04f
#define LEDANGLEY  0.06f

饶先宏's avatar
饶先宏 已提交
373 374
#define LEDOFFS    0.00f

饶先宏's avatar
饶先宏 已提交
375 376
static int digitled_DrawDigit(sDigitLed* pobj, int ind)
{
饶先宏's avatar
饶先宏 已提交
377
	float xoffs = -0.9f + (9-ind) * (LEDWIDTH + LEDTHINX * 5);
饶先宏's avatar
饶先宏 已提交
378 379 380 381 382
	float yoffs = -LEDHEIGHT / 2.0f + 0.2f;

	/* seg a */
	glColor3f(pobj->ledcolor[ind][0][0], pobj->ledcolor[ind][0][1], pobj->ledcolor[ind][0][2]);
	glBegin(GL_TRIANGLE_FAN);
饶先宏's avatar
饶先宏 已提交
383 384 385 386 387 388
	glVertex2f(xoffs + LEDOFFS + LEDGAPX,				yoffs + LEDHEIGHT);
	glVertex2f(xoffs + LEDOFFS + LEDANGLEX,			yoffs + LEDHEIGHT + LEDTHINY);
	glVertex2f(xoffs + LEDOFFS + LEDWIDTH - LEDANGLEX, yoffs + LEDHEIGHT + LEDTHINY);
	glVertex2f(xoffs + LEDOFFS + LEDWIDTH - LEDGAPX,	yoffs + LEDHEIGHT);
	glVertex2f(xoffs + LEDOFFS + LEDWIDTH - LEDANGLEX, yoffs + LEDHEIGHT - LEDTHINY);
	glVertex2f(xoffs + LEDOFFS + LEDANGLEX,			yoffs + LEDHEIGHT - LEDTHINY);
饶先宏's avatar
饶先宏 已提交
389 390 391 392 393
	glEnd();

	/* seg b */
	glColor3f(pobj->ledcolor[ind][1][0], pobj->ledcolor[ind][1][1], pobj->ledcolor[ind][1][2]);
	glBegin(GL_TRIANGLE_FAN);
饶先宏's avatar
饶先宏 已提交
394 395 396 397 398 399
	glVertex2f(xoffs + LEDOFFS + LEDWIDTH,			yoffs + LEDHEIGHT - LEDGAPY);
	glVertex2f(xoffs + LEDOFFS + LEDWIDTH + LEDTHINX, yoffs + LEDHEIGHT - LEDANGLEY);
	glVertex2f(xoffs + LEDOFFS / 2.0f + LEDWIDTH + LEDTHINX, yoffs + LEDHEIGHT / 2.0f + LEDANGLEY);
	glVertex2f(xoffs + LEDOFFS / 2.0f + LEDWIDTH,			yoffs + LEDHEIGHT / 2.0f + LEDGAPY);
	glVertex2f(xoffs + LEDOFFS / 2.0f + LEDWIDTH - LEDTHINX, yoffs + LEDHEIGHT / 2.0f + LEDANGLEY);
	glVertex2f(xoffs + LEDOFFS + LEDWIDTH - LEDTHINX, yoffs + LEDHEIGHT - LEDANGLEY);
饶先宏's avatar
饶先宏 已提交
400 401 402 403 404
	glEnd();

	/* seg c */
	glColor3f(pobj->ledcolor[ind][2][0], pobj->ledcolor[ind][2][1], pobj->ledcolor[ind][2][2]);
	glBegin(GL_TRIANGLE_FAN);
饶先宏's avatar
饶先宏 已提交
405 406
	glVertex2f(xoffs + LEDOFFS / 2.0f + LEDWIDTH, yoffs + LEDHEIGHT / 2.0f - LEDGAPY);
	glVertex2f(xoffs + LEDOFFS / 2.0f + LEDWIDTH + LEDTHINX, yoffs + LEDHEIGHT / 2.0f - LEDANGLEY);
饶先宏's avatar
饶先宏 已提交
407 408 409
	glVertex2f(xoffs + LEDWIDTH + LEDTHINX, yoffs + LEDANGLEY);
	glVertex2f(xoffs + LEDWIDTH, yoffs  + LEDGAPY);
	glVertex2f(xoffs + LEDWIDTH - LEDTHINX, yoffs + LEDANGLEY);
饶先宏's avatar
饶先宏 已提交
410
	glVertex2f(xoffs + LEDOFFS / 2.0f + LEDWIDTH - LEDTHINX, yoffs + LEDHEIGHT / 2.0f - LEDANGLEY);
饶先宏's avatar
饶先宏 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
	glEnd();

	/* seg d */
	glColor3f(pobj->ledcolor[ind][3][0], pobj->ledcolor[ind][3][1], pobj->ledcolor[ind][3][2]);
	glBegin(GL_TRIANGLE_FAN);
	glVertex2f(xoffs + LEDGAPX, yoffs);
	glVertex2f(xoffs + LEDANGLEX, yoffs + LEDTHINY);
	glVertex2f(xoffs + LEDWIDTH - LEDANGLEX, yoffs + LEDTHINY);
	glVertex2f(xoffs + LEDWIDTH - LEDGAPX, yoffs);
	glVertex2f(xoffs + LEDWIDTH - LEDANGLEX, yoffs - LEDTHINY);
	glVertex2f(xoffs + LEDANGLEX, yoffs - LEDTHINY);
	glEnd();

	/* seg e */
	glColor3f(pobj->ledcolor[ind][4][0], pobj->ledcolor[ind][4][1], pobj->ledcolor[ind][4][2]);
	glBegin(GL_TRIANGLE_FAN);
饶先宏's avatar
饶先宏 已提交
427 428
	glVertex2f(xoffs + LEDOFFS / 2.0f, yoffs + LEDHEIGHT / 2.0f - LEDGAPY);
	glVertex2f(xoffs + LEDOFFS / 2.0f + LEDTHINX, yoffs + LEDHEIGHT / 2.0f - LEDANGLEY);
饶先宏's avatar
饶先宏 已提交
429 430 431
	glVertex2f(xoffs + LEDTHINX, yoffs + LEDANGLEY);
	glVertex2f(xoffs, yoffs + LEDGAPY);
	glVertex2f(xoffs - LEDTHINX, yoffs + LEDANGLEY);
饶先宏's avatar
饶先宏 已提交
432
	glVertex2f(xoffs + LEDOFFS / 2.0f - LEDTHINX, yoffs + LEDHEIGHT / 2.0f - LEDANGLEY);
饶先宏's avatar
饶先宏 已提交
433 434 435 436 437
	glEnd();

	/* seg f */
	glColor3f(pobj->ledcolor[ind][5][0], pobj->ledcolor[ind][5][1], pobj->ledcolor[ind][5][2]);
	glBegin(GL_TRIANGLE_FAN);
饶先宏's avatar
饶先宏 已提交
438 439 440 441 442 443
	glVertex2f(xoffs + LEDOFFS, yoffs + LEDHEIGHT - LEDGAPY);
	glVertex2f(xoffs + LEDOFFS + LEDTHINX, yoffs + LEDHEIGHT - LEDANGLEY);
	glVertex2f(xoffs + LEDOFFS / 2.0f + LEDTHINX, yoffs + LEDHEIGHT / 2.0f + LEDANGLEY);
	glVertex2f(xoffs + LEDOFFS / 2.0f, yoffs + LEDHEIGHT / 2.0f + LEDGAPY);
	glVertex2f(xoffs + LEDOFFS / 2.0f - LEDTHINX, yoffs + LEDHEIGHT / 2.0f + LEDANGLEY);
	glVertex2f(xoffs + LEDOFFS - LEDTHINX, yoffs + LEDHEIGHT - LEDANGLEY);
饶先宏's avatar
饶先宏 已提交
444 445 446 447 448
	glEnd();

	/* seg g */
	glColor3f(pobj->ledcolor[ind][6][0], pobj->ledcolor[ind][6][1], pobj->ledcolor[ind][6][2]);
	glBegin(GL_TRIANGLE_FAN);
饶先宏's avatar
饶先宏 已提交
449 450 451 452 453 454
	glVertex2f(xoffs + LEDOFFS / 2.0f + LEDGAPX, yoffs + LEDHEIGHT / 2.0f);
	glVertex2f(xoffs + LEDOFFS / 2.0f + LEDANGLEX, yoffs + LEDHEIGHT / 2.0f + LEDTHINY);
	glVertex2f(xoffs + LEDOFFS / 2.0f + LEDWIDTH - LEDANGLEX, yoffs + LEDHEIGHT / 2.0f + LEDTHINY);
	glVertex2f(xoffs + LEDOFFS / 2.0f + LEDWIDTH - LEDGAPX, yoffs + LEDHEIGHT / 2.0f);
	glVertex2f(xoffs + LEDOFFS / 2.0f + LEDWIDTH - LEDANGLEX, yoffs + LEDHEIGHT / 2.0f - LEDTHINY);
	glVertex2f(xoffs + LEDOFFS / 2.0f + LEDANGLEX, yoffs + LEDHEIGHT / 2.0f - LEDTHINY);
饶先宏's avatar
饶先宏 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
	glEnd();

	/* seg dp */
	glColor3f(pobj->ledcolor[ind][7][0], pobj->ledcolor[ind][7][1], pobj->ledcolor[ind][7][2]);
	glBegin(GL_TRIANGLE_FAN);
	glVertex2f(xoffs + LEDWIDTH + LEDTHINX * 1.5, yoffs + 0.5 * LEDTHINY);
	glVertex2f(xoffs + LEDWIDTH + LEDTHINX * 3, yoffs - 1 * LEDTHINY);
	glVertex2f(xoffs + LEDWIDTH + LEDTHINX * 1.5, yoffs - 2.5 * LEDTHINY);
	glVertex2f(xoffs + LEDWIDTH + LEDTHINX * 0, yoffs - 1 * LEDTHINY);
	glEnd();

	/* keyboard status */
	if (pobj->keypressed & (1 << ind)) {
		glColor3f(1.0f, 0.0f, 0.0f);
	}
	else {
		glColor3f(0.0f, 1.0f, 0.0f);
	}

	glBegin(GL_TRIANGLE_FAN);
	glVertex2f(xoffs + LEDTHINX, yoffs - LEDTHINY * 8);
	glVertex2f(xoffs + LEDTHINX, yoffs - LEDTHINY * 13);
	glVertex2f(xoffs + LEDWIDTH - LEDTHINX, yoffs - LEDTHINY * 13);
	glVertex2f(xoffs + LEDWIDTH - LEDTHINX, yoffs - LEDTHINY * 8);
	glEnd();
	return 0;
}


static int digitled_Render(sDigitLed* pobj)
{
	int i;
	glfwGetFramebufferSize(pobj->window, &pobj->width, &pobj->height);
	glViewport(0, 0, pobj->width, pobj->height);
	glClearColor(0.0f, 0.0f, 0.2f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	for (i = 0; i < LEDCOUNT; i++) {
		digitled_DrawDigit(pobj, i);
	}
	return 1;
}

int StopRunning();

static int digitled_hdl4se_unit_Setup(HOBJECT object)
{
	sDigitLed* pobj;
	pobj = (sDigitLed*)objectThis(object);
	/*读信号和读地址寄存一拍*/
	pobj->wRead = pobj->wRead_cur;
	pobj->bReadAddr = pobj->bReadAddr_cur;
饶先宏's avatar
饶先宏 已提交
510 511 512 513 514 515 516
	pobj->count++;
	/* 每256个周期新绘制一次 */
	if ((pobj->count & 0xff) == 0) {
		if (digitled_Render(pobj)) {
			/* Swap buffers */
			glfwSwapBuffers(pobj->window);
		}
饶先宏's avatar
饶先宏 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
	}

	glfwPollEvents();

	/* Check if we are still running */
	if (glfwWindowShouldClose(pobj->window))
		StopRunning();
	return 0;
}

IHDL4SEUnit** guiCreate(unsigned int baseaddr, const char * name)
{
	int ret;
	IHDL4SEUnit** gui;
	PARAMITEM param[2];
	IHDL4SEUnit** result = NULL;
	param[0].name = PARAMID_BASEADDR;
	param[0].i32value = baseaddr;
	param[1].name = PARAMID_HDL4SE_UNIT_NAME;
	param[1].pvalue = name;
	A_u_t_o_registor_digitled();
	ret = objectCreateEx(CLSID_DIGITLED, param, 2, IID_HDL4SEUNIT, (const void**)&gui);
	return gui;
}