File size: 24,807 Bytes
00e6582
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*
Copyright (c) 2026 Ethan Mortonson.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "emulator.h"
#include "new_opcodes.h"
#include "time.h"
struct _Computer* Create_Computer()
{
    struct _Computer* Computer = (struct _Computer*) calloc(1,sizeof (struct _Computer));
	return Computer;
};

void Initialize_Computer(struct _Computer* Computer)
{
    srand(time(NULL));

    Computer->Red_Level = 1;
    Computer->Blue_Level = 0.1;
    Computer->Green_Level = 1;

    Computer->Processor.Register_Zero = 0;
    Computer->Processor.Register_One = 0;

    Computer->Processor.Stack_Pointer = 6550;
    Computer->Processor.Base_Pointer = 6550;
    //printf("SP: %d\n", Computer->Processor.Stack_Pointer);

    Computer->Processor.Instruction_Pointer = 0;
    Computer->Processor.Zero_Flag = false;

    for(int j = 0; j < 65536; j++)
    {
        Computer->Video_RAM_Front_Buffer[j] = 0;
        Computer->Video_RAM_Back_Buffer[j] = 0;
    }

    for(int l = 0; l < 256; l++)
    {
        for(int j = 0; j < 65536; j++)
        {
            Computer->RAM[l].Bank[j] = 0;
        }
    }

    Computer->HDD_Slot_Status = 1;
    Computer->Removable_Slot_Status = 0;

    Computer->Sensor_Link_Port_Status = 0;
    Computer->Peripheral_Link_Port_Status = 0;

    Computer->Network_Link_Port_Status = 0;

    Computer->Monitor_Link_Port_Status = 0;

    Computer->Keyboard_Link_Port_Status = 0;
    Computer->Mouse_Link_Port_Status = 0;
    Computer->Controller_Link_Port_Status = 0;

    Computer->Power_Status = 0;
    Computer->Power_Supply_Status = 1;
    Computer->Fan_Status = 0;


    Computer->Controller_RAM[0] = 0; //X and Y hats, 4 bytes. 1 byte for 4 buttons and a DPAD.
    Computer->Controller_RAM[1] = 0; //X and Y hats, 4 bytes. 1 byte for 4 buttons and a DPAD.
    Computer->Controller_RAM[2] = 0; //X and Y hats, 4 bytes. 1 byte for 4 buttons and a DPAD.
    Computer->Controller_RAM[3] = 0; //X and Y hats, 4 bytes. 1 byte for 4 buttons and a DPAD.
    Computer->Controller_RAM[4] = 0; //X and Y hats, 4 bytes. 1 byte for 4 buttons and a DPAD.

    for(int j = 0; j < 32; j++)
    {
        Computer->Keyboard_RAM[j] = 0;
        Computer->External_Lights_RAM[j] = 0;
        Computer->External_Inputs_RAM[j] = 0;
    }

    Computer->Mouse_RAM[0] = 0;
    Computer->Mouse_RAM[1] = 0;
    Computer->Mouse_RAM[2] = 0;


    for(int j = 0; j < 256*256*4; j++)
    {
        Computer->True_Video_Screen[j] =0;
    }

    Computer->Color_Mode = VIDEO_MODE_MONOCHROME;
    Computer->Background_Color = 128;

    glGenTextures(1, &Computer->Texture_ID);
    glBindTexture(GL_TEXTURE_2D, Computer->Texture_ID);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256,256, 0, GL_RGBA, GL_UNSIGNED_BYTE, Computer->True_Video_Screen);
    glEnable(GL_TEXTURE_2D);

};


void Turn_On_Computer(struct _Computer* Computer)
{
    Computer->Power_Status = 1;
    //printf("Emulator on\n");
};

void Handle_Computer_Inputs(struct _Computer* Computer, struct _Keypad Keypad)
{
    if(Keypad.Keyboard_Up)
    {
        Computer->Controller_RAM[0] = 1;
    }
    if(Keypad.Keyboard_Down)
    {
        Computer->Controller_RAM[1] = 1;
    }
    if(Keypad.Keyboard_Left)
    {
        Computer->Controller_RAM[2] = 1;
    }
    if(Keypad.Keyboard_Right)
    {
        Computer->Controller_RAM[3] = 1;
    }
}

void Turn_Off_Computer(struct _Computer* Computer)
{
    if(Computer->Power_Status == 1)
    {
    Computer->Power_Status = 0;

    Computer->Processor.Register_Zero = 0;
    Computer->Processor.Register_One = 0;

    Computer->Processor.Stack_Pointer = 65535;
    Computer->Processor.Base_Pointer = 65535;

    Computer->Processor.Instruction_Pointer = 0;
    Computer->Processor.Zero_Flag = false;

    for(int j = 0; j < 65536; j++)
    {
        Computer->Video_RAM_Front_Buffer[j] = 0;
        Computer->Video_RAM_Back_Buffer[j] = 0;
    }

    for(int l = 0; l < 256; l++)
    {
        for(int j = 0; j < 65536; j++)
        {
            Computer->RAM[l].Bank[j] = 0;
        }
    }

    Computer->HDD_Slot_Status = 1;
    Computer->Removable_Slot_Status = 0;

    Computer->Sensor_Link_Port_Status = 0;
    Computer->Peripheral_Link_Port_Status = 0;

    Computer->Network_Link_Port_Status = 0;

    Computer->Monitor_Link_Port_Status = 0;

    Computer->Keyboard_Link_Port_Status = 0;
    Computer->Mouse_Link_Port_Status = 0;
    Computer->Controller_Link_Port_Status = 0;

    Computer->Power_Status = 0;
    Computer->Power_Supply_Status = 1;
    Computer->Fan_Status = 0;


    Computer->Controller_RAM[0] = 0; //X and Y hats, 4 bytes. 1 byte for 4 buttons and a DPAD.
    Computer->Controller_RAM[1] = 0; //X and Y hats, 4 bytes. 1 byte for 4 buttons and a DPAD.
    Computer->Controller_RAM[2] = 0; //X and Y hats, 4 bytes. 1 byte for 4 buttons and a DPAD.
    Computer->Controller_RAM[3] = 0; //X and Y hats, 4 bytes. 1 byte for 4 buttons and a DPAD.
    Computer->Controller_RAM[4] = 0; //X and Y hats, 4 bytes. 1 byte for 4 buttons and a DPAD.

    for(int j = 0; j < 32; j++)
    {
        Computer->Keyboard_RAM[j] = 0;
        Computer->External_Lights_RAM[j] = 0;
        Computer->External_Inputs_RAM[j] = 0;
    }

    Computer->Mouse_RAM[0] = 0;
    Computer->Mouse_RAM[1] = 0;
    Computer->Mouse_RAM[2] = 0;


    for(int j = 0; j < 256*256*4; j++)
    {
        Computer->True_Video_Screen[j] = 0;
    }

    }
};

void Load_Program_Into_RAM_And_Start(struct _Computer* Computer, const char* Filename)
{
    FILE* f = fopen(Filename, "rb");
    unsigned char Header[4];
    fread(&Header, 1, 4, f);

    unsigned char Banks_On_Disk = *(unsigned char*)&Header[0];
    unsigned int Bytes_To_Load = *(unsigned char*)&Header[0] * sizeof(struct _SixtyFourK_Bank); //NUmber of banks * sizeof Bank

    //printf("Need to load %d bytes.\n", Bytes_To_Load);

    fseek(f, 4, SEEK_SET);

    for(unsigned char j = 0; j < Banks_On_Disk; j++)
    {
        //printf("Bank: %d\n", j);
        fread(&Computer->RAM[j],1,sizeof(struct _SixtyFourK_Bank),f);
        //printf("Loaded a Bank");
    }
    fclose(f);
    //printf("Loaded ROM");
    Computer->CPU_Is_Running = 1;
    Computer->Current_RAM_Bank = Header[1];
    Computer->Processor.Instruction_Pointer = (Header[3] << 8) | (Header[2] & 0xff);

    Computer->RAM[0].Bank[0] = 0;
    Computer->RAM[0].Bank[1] = 0;
    Computer->RAM[0].Bank[2] = 0;
    Computer->RAM[0].Bank[3] = 0;

    //printf("Starting execution at Bank %d, Address %d\n", Header[1], Computer->Processor.Instruction_Pointer);
};


void Load_BIOS_Into_RAM_And_Start(struct _Computer* Computer, const char* Filename)
{

};

void Process_Computer(struct _Computer* Computer, int Maximum_Cycles, struct _Keypad Keypad)
{
    if(Computer->Power_Status == 1)
    {
    if(Computer->CPU_Is_Running == 1)
    {
        int Cycles = 0;
        while(Cycles < Maximum_Cycles)
        {
        //Run_New_Opcode_Clear_Screen(Computer);
        Run_A_Processing_Cycle(Computer, Keypad);
        //Update_Computer_Video_Card(Computer);
       // Computer->Cycles += 1;
        ////printf("Cycles: %d\n", Cycles);
        Cycles += 1;
        //60 cycles/second 60Hz
        };
    }
    }
}

void Run_A_Processing_Cycle(struct _Computer* Computer, struct _Keypad Keypad)
{
    unsigned char Current_Opcode = Computer->RAM[Computer->Current_RAM_Bank].Bank[Computer->Processor.Instruction_Pointer];
    ////printf("r0: %d r1: %d\n", Computer->Processor.Register_Zero, Computer->Processor.Register_One);
    ////printf("The current IP is %d\n", Computer->Processor.Instruction_Pointer);
   // //printf("SP: %d\n", Computer->Processor.Stack_Pointer);
    /////printf("Contents of Stack: %d %d %d %d ", Computer->RAM[0].Bank[Computer->Processor.Stack_Pointer-4],Computer->RAM[0].Bank[Computer->Processor.Stack_Pointer-3],Computer->RAM[0].Bank[Computer->Processor.Stack_Pointer-2],Computer->RAM[0].Bank[Computer->Processor.Stack_Pointer-1]);
   // //printf("%d %d %d %d %d %d %d %d %d %d\n", Computer->RAM[0].Bank[Computer->Processor.Stack_Pointer],Computer->RAM[0].Bank[Computer->Processor.Stack_Pointer+1],Computer->RAM[0].Bank[Computer->Processor.Stack_Pointer+2],Computer->RAM[0].Bank[Computer->Processor.Stack_Pointer+3],Computer->RAM[0].Bank[Computer->Processor.Stack_Pointer+4],Computer->RAM[0].Bank[Computer->Processor.Stack_Pointer+5],Computer->RAM[0].Bank[Computer->Processor.Stack_Pointer+6],Computer->RAM[0].Bank[Computer->Processor.Stack_Pointer+7],Computer->RAM[0].Bank[Computer->Processor.Stack_Pointer+8],Computer->RAM[0].Bank[Computer->Processor.Stack_Pointer+9]);
    Update_Computer_Inputs(Computer, Keypad);
    switch(Current_Opcode)
    {
        case NEW_OPCODE_NOP:
            Run_New_Opcode_NOP(Computer);
        break;

        case NEW_OPCODE_MOV_RZero_RZero:
            Run_New_Opcode_MOV_RZero_RZero(Computer);
        break;

        case NEW_OPCODE_MOV_RZero_ROne:
            Run_New_Opcode_MOV_RZero_ROne(Computer);
        break;

        case NEW_OPCODE_MOV_ROne_RZero:
            Run_New_Opcode_MOV_ROne_RZero(Computer);
        break;

        case NEW_OPCODE_MOV_ROne_ROne:
            Run_New_Opcode_MOV_ROne_ROne(Computer);
        break;

        case NEW_OPCODE_MOV_RZero_ADDR:
            Run_New_Opcode_MOV_RZero_ADDR(Computer);
        break;
        case NEW_OPCODE_MOV_ROne_ADDR:
            Run_New_Opcode_MOV_ROne_ADDR(Computer);
        break;

        case NEW_OPCODE_MOV_ADDR_RZero:
            Run_New_Opcode_MOV_ADDR_RZero(Computer);
        break;
        case NEW_OPCODE_MOV_ADDR_ROne:
            Run_New_Opcode_MOV_ADDR_ROne(Computer);
        break;

        case NEW_OPCODE_PUSH_RZero:
            Run_New_Opcode_PUSH_RZero(Computer);
        break;
        case NEW_OPCODE_PUSH_ROne:
            Run_New_Opcode_PUSH_ROne(Computer);
        break;

        case NEW_OPCODE_POP_RZero:
            Run_New_Opcode_POP_RZero(Computer);
        break;
        case NEW_OPCODE_POP_ROne:
            Run_New_Opcode_POP_ROne(Computer);
        break;

        case NEW_OPCODE_ADD_RZero_RZero:
            Run_New_Opcode_ADD_RZero_RZero(Computer);
        break;
        case NEW_OPCODE_ADD_RZero_ROne:
            Run_New_Opcode_ADD_RZero_ROne(Computer);
        break;
        case NEW_OPCODE_ADD_ROne_RZero:
            Run_New_Opcode_ADD_ROne_RZero(Computer);
        break;
        case NEW_OPCODE_ADD_ROne_ROne:
            Run_New_Opcode_ADD_ROne_ROne(Computer);
        break;

        case NEW_OPCODE_SUB_RZero_RZero:
            Run_New_Opcode_SUB_RZero_RZero(Computer);
        break;
        case NEW_OPCODE_SUB_RZero_ROne:
            Run_New_Opcode_SUB_RZero_ROne(Computer);
        break;
        case NEW_OPCODE_SUB_ROne_RZero:
            Run_New_Opcode_SUB_ROne_RZero(Computer);
        break;
        case NEW_OPCODE_SUB_ROne_ROne:
            Run_New_Opcode_SUB_ROne_ROne(Computer);
        break;
        case NEW_OPCODE_MUL_RZero_RZero:
            Run_New_Opcode_MUL_RZero_RZero(Computer);
        break;
        case NEW_OPCODE_MUL_RZero_ROne:
            Run_New_Opcode_MUL_RZero_ROne(Computer);
        break;
        case NEW_OPCODE_MUL_ROne_RZero:
            Run_New_Opcode_MUL_ROne_RZero(Computer);
        break;
        case NEW_OPCODE_MUL_ROne_ROne:
            Run_New_Opcode_MUL_ROne_ROne(Computer);
        break;

        case NEW_OPCODE_DIV_RZero_RZero:
            Run_New_Opcode_DIV_RZero_RZero(Computer);
        break;
        case NEW_OPCODE_DIV_RZero_ROne:
            Run_New_Opcode_DIV_RZero_ROne(Computer);
        break;
        case NEW_OPCODE_DIV_ROne_RZero:
            Run_New_Opcode_DIV_ROne_RZero(Computer);
        break;
        case NEW_OPCODE_DIV_ROne_ROne:
            Run_New_Opcode_DIV_ROne_ROne(Computer);
        break;

        case NEW_OPCODE_PWR_RZero_RZero:
            Run_New_Opcode_PWR_RZero_RZero(Computer);
        break;
        case NEW_OPCODE_PWR_RZero_ROne:
            Run_New_Opcode_PWR_RZero_ROne(Computer);
        break;
        case NEW_OPCODE_PWR_ROne_RZero:
            Run_New_Opcode_PWR_ROne_RZero(Computer);
        break;
        case NEW_OPCODE_PWR_ROne_ROne:
            Run_New_Opcode_PWR_ROne_ROne(Computer);
        break;

        case NEW_OPCODE_ROOT_RZero_RZero:
            Run_New_Opcode_ROOT_RZero_RZero(Computer);
        break;
        case NEW_OPCODE_ROOT_RZero_ROne:
            Run_New_Opcode_ROOT_RZero_ROne(Computer);
        break;
        case NEW_OPCODE_ROOT_ROne_RZero:
            Run_New_Opcode_ROOT_ROne_RZero(Computer);
        break;
        case NEW_OPCODE_ROOT_ROne_ROne:
            Run_New_Opcode_ROOT_ROne_ROne(Computer);
        break;

        case NEW_OPCODE_JMP_ADDRSIXTEEN:
            Run_New_Opcode_JMP(Computer);
        break;
        case NEW_OPCODE_JE_ADDRSIXTEEN:
            Run_New_Opcode_JE(Computer);
        break;
        case NEW_OPCODE_JNE_ADDRSIXTEEN:
            Run_New_Opcode_JNE(Computer);
        break;
        case NEW_OPCODE_CALL_ADDRSIXTEEN:
            Run_New_Opcode_CALL(Computer);
        break;

        case NEW_OPCODE_RET:
            Run_New_Opcode_RET(Computer);
        break;
        case NEW_OPCODE_HALT:
            Run_New_Opcode_HALT(Computer);
        break;


        case NEW_OPCODE_MOV_RZero_Value:
            Run_New_Opcode_MOV_RZero_Value(Computer);
        break;
        case NEW_OPCODE_MOV_ROne_Value:
            Run_New_Opcode_MOV_ROne_Value(Computer);
        break;


        case NEW_OPCODE_CMP_RZero_Value:
            Run_New_Opcode_CMP_RZero_Value(Computer);
        break;
        case NEW_OPCODE_CMP_ROne_Value:
            Run_New_Opcode_CMP_ROne_Value(Computer);
        break;

        case NEW_OPCODE_CMP_RZero_ROne:
            Run_New_Opcode_CMP_RZero_ROne(Computer);
        break;
        case NEW_OPCODE_CMP_ROne_RZero:
            Run_New_Opcode_CMP_ROne_RZero(Computer);
        break;

        case NEW_OPCODE_IN:
            Run_New_Opcode_IN(Computer);
        break;
        case NEW_OPCODE_OUT:
            Run_New_Opcode_OUT(Computer);
        break;
        case NEW_OPCODE_REP:
            Run_New_Opcode_REP(Computer);
        break;

//FLAGS
        case NEW_OPCODE_SET_ZERO_FLAG:
            Run_New_Opcode_SET_ZERO_FLAG(Computer);
        break;
        case NEW_OPCODE_SET_CARRY_FLAG:
            Run_New_Opcode_SET_CARRY_FLAG(Computer);
        break;
        case NEW_OPCODE_SET_SIGN_FLAG:
            Run_New_Opcode_SET_SIGN_FLAG(Computer);
        break;
        case NEW_OPCODE_SET_OVERFLOW_FLAG:
            Run_New_Opcode_SET_OVERFLOW_FLAG(Computer);
        break;
        case NEW_OPCODE_SET_PARITY_FLAG:
            Run_New_Opcode_SET_PARITY_FLAG(Computer);
        break;
        case NEW_OPCODE_SET_AUXILIARY_CARRY_FLAG:
            Run_New_Opcode_SET_AUXILIARY_CARRY_FLAG(Computer);
        break;
        case NEW_OPCODE_SET_DIRECTION_FLAG:
            Run_New_Opcode_SET_DIRECTION_FLAG(Computer);
        break;
        case NEW_OPCODE_SET_MASKABLE_INTERRUPT_FLAG:
            Run_New_Opcode_SET_MASKABLE_INTERRUPT_FLAG(Computer);
        break;
        case NEW_OPCODE_SET_TRAP_FLAG:
            Run_New_Opcode_SET_TRAP_FLAG(Computer);
        break;

        case NEW_OPCODE_ERROR_HANDLER:
            Run_New_Opcode_Error_Handler(Computer);
        break;

        case NEW_OPCODE_RENDER_SPRITE:
            Run_New_Opcode_Render_Sprite(Computer);
        break;



        case NEW_OPCODE_PUSH_RZero_HI:
            Run_New_Opcode_PUSH_RZero_HI(Computer);
        break;
        case NEW_OPCODE_PUSH_ROne_HI:
            Run_New_Opcode_PUSH_ROne_HI(Computer);
        break;

        case NEW_OPCODE_POP_RZero_LO:
            Run_New_Opcode_POP_RZero_LO(Computer);
        break;
        case NEW_OPCODE_POP_ROne_LO:
            Run_New_Opcode_POP_ROne_LO(Computer);
        break;

        case NEW_OPCODE_RENDER_POINT:
            Run_New_Opcode_Render_Point(Computer);
        break;

        case NEW_OPCODE_RENDER_LINE:
            Run_New_Opcode_Render_Line(Computer);
        break;

        case NEW_OPCODE_RENDER_TRIANGLE:
            Run_New_Opcode_Render_Triangle(Computer);
        break;

        case NEW_OPCODE_RENDER_TILEMAP:
            Run_New_Opcode_Render_Tilemap(Computer);
        break;

         case NEW_OPCODE_CLEAR_SCREEN:
            Run_New_Opcode_Clear_Screen(Computer);
        break;

        case NEW_OPCODE_FLIP_SCREEN:
            Run_New_Opcode_Flip_Screen(Computer);
        break;


        case NEW_OPCODE_MOV_RONE_RANDOM:
            Run_New_Opcode_MOV_ROne_Random(Computer);
        break;

        case NEW_OPCODE_MOV_RZERO_RANDOM:
            Run_New_Opcode_MOV_RZero_Random(Computer);
        break;



        case NEW_OPCODE_INC_RZero:
            Run_NEW_OPCODE_INC_RZero(Computer);
        break;

        case NEW_OPCODE_INC_ROne:
            Run_NEW_OPCODE_INC_ROne(Computer);
        break;

        case NEW_OPCODE_JMP_BANK_ADDRSIXTEEN:
            Run_NEW_OPCODE_JMP_BANK_ADDRSIXTEEN(Computer);
        break;

        case NEW_OPCODE_JE_BANK_ADDRSIXTEEN:
            Run_NEW_OPCODE_JE_BANK_ADDRSIXTEEN(Computer);
        break;

        case NEW_OPCODE_PUSH_BANK_ADDRSIXTEEN:
            Run_New_Opcode_PUSH_BANK_ADDRSIXTEEN(Computer);
        break;

        case NEW_OPCODE_JMP_STACK:
            Run_New_Opcode_JMP_STACK(Computer);
        break;

        case NEW_OPCODE_CMP_STACK:
            Run_New_Opcode_CMP_STACK(Computer);
        break;

        case NEW_OPCODE_INC_ON_UP_KEY:
            Run_New_Opcode_INC_ON_UP_KEY(Computer);
        break;

        case NEW_OPCODE_DEC_ON_DOWN_KEY:
            Run_New_Opcode_DEC_ON_DOWN_KEY(Computer);
        break;

        case NEW_OPCODE_DEC_ON_LEFT_KEY:
            Run_New_Opcode_DEC_ON_LEFT_KEY(Computer);
        break;

        case NEW_OPCODE_INC_ON_RIGHT_KEY:
            Run_New_Opcode_INC_ON_RIGHT_KEY(Computer);
        break;

        default:
            Run_New_Opcode_Error_Handler(Computer);
        break;
    }
};

void Update_Computer_Inputs(struct _Computer* Computer, struct _Keypad Keypad)
{
    if(Keypad.Keyboard_Up)
    {
        Computer->Controller_RAM[0] = 1;
    }

    else if(!Keypad.Keyboard_Up)
    {
        Computer->Controller_RAM[0] = 0;
    }




    if(Keypad.Keyboard_Down)
    {
        Computer->Controller_RAM[1] = 1;
    }
    if(!Keypad.Keyboard_Down)
    {
        Computer->Controller_RAM[1] = 0;
    }




    if(Keypad.Keyboard_Left)
    {
        Computer->Controller_RAM[2] = 1;
    }
    else if(!Keypad.Keyboard_Left)
    {
        Computer->Controller_RAM[2] = 0;
    }





    if(Keypad.Keyboard_Right)
    {
        Computer->Controller_RAM[3] = 1;
    }
    if(!Keypad.Keyboard_Right)
    {
        Computer->Controller_RAM[3] = 0;
    }
};

void Update_Computer_Video_Card(struct _Computer* Computer)
{
    //glGenTextures(1, &Computer->Texture_ID);
    glBindTexture(GL_TEXTURE_2D, Computer->Texture_ID);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    int in = 0;
    int r = 0;
    //Image->RGBA_Canvas[0] = Image->RGB_Canvas[0];

    unsigned char Fourth_Color = 255;

    unsigned char Red_mask = 0xC0;
    unsigned char Green_mask = 0x30;
    unsigned char Blue_mask = 0x0C;
    unsigned char Transparency_Level_Mask = 0x03;

    ////printf("Updating Video Card\n");
    if(Computer->Color_Mode == VIDEO_MODE_MONOCHROME)
    {
    for(int j = 0; j < 65536; j++)
    {
        ////printf("Render");

        if(Computer->Video_RAM_Back_Buffer[in] < 255)
        {
        memcpy(&Computer->True_Video_Screen[r], &Computer->Video_RAM_Back_Buffer[in], 1);
        memcpy(&Computer->True_Video_Screen[r+1], &Computer->Video_RAM_Back_Buffer[in], 1);
        memcpy(&Computer->True_Video_Screen[r+2], &Computer->Video_RAM_Back_Buffer[in], 1);
        Computer->True_Video_Screen[r+3] = 255;

       // Computer->True_Video_Screen[r] = Computer->True_Video_Screen[r]*Computer->Red_Level;
       // Computer->True_Video_Screen[r+1] = Computer->True_Video_Screen[r+1]*Computer->Green_Level;
      //  Computer->True_Video_Screen[r+2] = Computer->True_Video_Screen[r+2]*Computer->Blue_Level;
        }

        in += 1;
        r += 4;
    }
    }



    int Shade_Of_Red;
    int Shade_Of_Green;
    int Shade_Of_Blue;
    int Transparency;

    if(Computer->Color_Mode == VIDEO_MODE_COLOR)
    {
    for(int j = 0; j < ((256*256*4)/4); j++)
    {
        Shade_Of_Red = Computer->Video_RAM_Back_Buffer[in] >> 6;
        Shade_Of_Green = Computer->Video_RAM_Back_Buffer[in] >> 4;
        Shade_Of_Blue = Computer->Video_RAM_Back_Buffer[in] >> 2;
        Transparency = Computer->Video_RAM_Back_Buffer[in] & 0x03;

        //unsigned char Transparency_Level_Mask = masked_Transparent_bits >> 0;

        ////printf("R %u\n G %u\n B %u\n", Shade_Of_Red, Shade_Of_Green, Shade_Of_Blue);


        Computer->True_Video_Screen[r] = (unsigned char)Shade_Of_Red;
        Computer->True_Video_Screen[r + 1] = (unsigned char)Shade_Of_Green;
        Computer->True_Video_Screen[r + 2] = (unsigned char)Shade_Of_Blue;
        Computer->True_Video_Screen[r + 3] = (unsigned char)Transparency;

        //Computer->True_Video_Screen[r + 3] = Fourth_Color;
        in += 1;
        r += 4;
    }
    }


    //Copy_Section(4,Section,Computer->True_Video_Screen,24,48,256,256,0,0,0,32,24,48);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256,256, 0, GL_RGBA, GL_UNSIGNED_BYTE, Computer->True_Video_Screen);
    glEnable(GL_TEXTURE_2D);
};

void Render_Computer(struct _Computer* Computer, float x, float y)
{
    glBindTexture(GL_TEXTURE_2D, Computer->Texture_ID);

    float Vertices[] = {x, y, x + 256, y, x + 256, y + 256, x, y + 256};
    float Texture_Vertices[] = {0, 0, 1, 0, 1, -1, 0, -1};
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY_EXT);
    glVertexPointer(2, GL_FLOAT, 0, Vertices);
    glTexCoordPointer(2, GL_FLOAT, 0, Texture_Vertices);
    glDrawArrays(GL_QUADS, 0, 4);
    glDisableClientState(GL_VERTEX_ARRAY);
};


void Render_Computer_Vertices(struct _Computer* Computer, float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4)
{
    glBindTexture(GL_TEXTURE_2D, Computer->Texture_ID);

    float Vertices[] = {x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4};
    float Texture_Vertices[] = {0, 0, 1, 0, 1, -1, 0, -1};
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY_EXT);
    glVertexPointer(3, GL_FLOAT, 0, Vertices);
    glTexCoordPointer(2, GL_FLOAT, 0, Texture_Vertices);
    glDrawArrays(GL_QUADS, 0, 4);
    glDisableClientState(GL_VERTEX_ARRAY);
};



void Create_Cartridge(unsigned char Number_Of_Banks, const char* Output_Filename)
{
    struct _Memory_Cartridge Cartridge;

    Cartridge.Number_Of_Banks = Number_Of_Banks;
    Cartridge.Offset_To_Banks = 2;

    struct _SixtyFourK_Bank Bank;

    for(int j = 0; j < 65536; j++)
    {
        Bank.Bank[j] = 0x00;
    }


    FILE* f;
    f = fopen(Output_Filename,"wb");
    fwrite(&Cartridge,1,sizeof(struct _Memory_Cartridge),f);
    rewind(f);

    fseek(f, Cartridge.Offset_To_Banks, SEEK_SET);

    for(int j = 0; j < Cartridge.Number_Of_Banks; j++)
    {
        fwrite(&Bank, 1, sizeof(struct _SixtyFourK_Bank), f);
    }
    fclose(f);
};

void Convert_Code_To_Cartridge_Data(struct _Memory_Cartridge* Cartridge, unsigned char Bank_ID, unsigned short Bank_Offset, const char* Filename)
{

};

void Convert_BMP_To_Cartridge_Data(struct _Memory_Cartridge* Cartridge, unsigned char Bank_ID, unsigned short Bank_Offset, const char* Filename)
{

};

void Convert_Data_To_Cartridge_Data(struct _Memory_Cartridge* Cartridge, unsigned char Bank_ID, unsigned short Bank_Offset, const char* Filename)
{

};