-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip328.c
124 lines (108 loc) · 3.36 KB
/
chip328.c
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
/*
* gcc chip328.c chip328lib.c -o chip328 -lglut -lGL -lGLU -I/chip328.c
*/
//#include <windows.h> // for MS Windows
#if !defined(__AVR_ATmega328P__)
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "chip328lib.h"
int cycleCount = 0;
int refreshMillis = 1;
uint8_t chip328Display[8][32];
uint8_t interfacePutPixel(uint8_t x, uint8_t y, uint8_t pixel){
uint8_t offsetByte=x/8;
uint8_t offsetBit=x%8;
uint8_t collision = 0;
if(pixel){
if(chip328Display[offsetByte][y]&(0b10000000>>offsetBit)) collision = 1;
chip328Display[offsetByte][y] = chip328Display[offsetByte][y]^(0b10000000>>offsetBit);
}
return collision;
}
//Main function. Inicia e configura o openGL
void interfaceDelayTimer(int value){
if(DT!=0) DT--;
glutTimerFunc(17, interfaceDelayTimer, 0);
}
void interfaceLoadROM(){
int i;
uint8_t temp[280];
FILE *rom;
rom = fopen("BRIX", "r");
int ab;
for(ab=0;ab<280;ab++){
fread(&temp,280,1,rom);
}
fclose(rom);
for(i=0;i<280;i++){
chip328Memory[i]=temp[i];
}
}
void interfaceDebug(){
printf("Ciclo: %d\n\n",cycleCount);
printf("PC: %x PC_Value: %x %x SP_: %x I: %x\n\n",PC,chip328MemoryRead(PC),chip328MemoryRead(PC+1),SP_,I);
printf("V0:%x V1:%x V2:%x V3:%x V4:%x V5:%x V6:%x V7:%x\n\n",V[0],V[1],V[2],V[3],V[4],V[5],V[6],V[7]);
printf("V8:%x V9:%x VA:%x VB:%x VC:%x VD:%x VE:%x VF:%x\n\n",V[8],V[9],V[10],V[11],V[12],V[13],V[14],V[15]);
//for(int i = 4080;i<4096;i++){
// printf("%x ",chip328Memory[i]);
//}
/*printf("\n\n");
for(int i = 1812;i<2000;i++){
printf("%d:0x%x ",i,chip328Memory[i]);
}*/
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
cycleCount++;
//if(cycleCount>885) getchar();
}
void putPixel(int x, int y){
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(-1+0.03125*x, 1-0.0625*(y+1));
glVertex2f(-1+0.03125*(x+1), 1-0.0625*(y+1));
glVertex2f(-1+0.03125*(x+1), 1-0.0625*y);
glVertex2f(-1+0.03125*x, 1-0.0625*y);
glEnd();
}
void interfaceDisplayWrite(){
int i=0,j=0;
for(j=0;j<32;j++){
for(i=0;i<8;i++){
if((chip328Display[i][j]>>7)&0b00000001) putPixel(0+i*8,j);
if((chip328Display[i][j]>>6)&0b00000001) putPixel(1+i*8,j);
if((chip328Display[i][j]>>5)&0b00000001) putPixel(2+i*8,j);
if((chip328Display[i][j]>>4)&0b00000001) putPixel(3+i*8,j);
if((chip328Display[i][j]>>3)&0b00000001) putPixel(4+i*8,j);
if((chip328Display[i][j]>>2)&0b00000001) putPixel(5+i*8,j);
if((chip328Display[i][j]>>1)&0b00000001) putPixel(6+i*8,j);
if((chip328Display[i][j])&0b00000001) putPixel(7+i*8,j);
}
}
}
void mainLoop(int value) {
interfaceDebug();
chip328Emulate();
interfaceDisplayWrite();
glutPostRedisplay(); // Post a paint request to activate display()
glutTimerFunc(refreshMillis, mainLoop, 0); // subsequent timer call at milliseconds
}
void display() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
interfaceDisplayWrite();
glFlush();
}
int main(int argc, char** argv) {
chip328Begin();
interfaceLoadROM();
glutInit(&argc, argv);
glutInitWindowSize(960, 480);
glutCreateWindow("Chip-328 Emulator");
glutInitWindowPosition(0, 0);
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutTimerFunc(0, mainLoop, 0);
glutTimerFunc(0, interfaceDelayTimer, 0);
glutMainLoop(); // Enter the event-processing loop
return 0;
}
#endif