2015-07-17 00:40:46 +00:00
|
|
|
//
|
|
|
|
// Atari2600.m
|
|
|
|
// ElectrEm
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 14/07/2015.
|
|
|
|
// Copyright © 2015 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "Atari2600.h"
|
|
|
|
#import "Atari2600.hpp"
|
2015-07-25 00:37:08 +00:00
|
|
|
#import <OpenGL/gl3.h>
|
|
|
|
|
|
|
|
const char *vertexShader =
|
|
|
|
"#version 150\n"
|
|
|
|
"\n"
|
|
|
|
"in vec2 position;\n"
|
|
|
|
"\n"
|
|
|
|
"out vec4 colour;\n"
|
|
|
|
"\n"
|
|
|
|
"void main (void)\n"
|
|
|
|
"{\n"
|
|
|
|
"colour = vec4(position, 0.0, 1.0);\n"
|
|
|
|
"gl_Position = vec4(position, 0.0, 1.0);\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
const char *fragmentShader =
|
|
|
|
"#version 150\n"
|
|
|
|
"\n"
|
|
|
|
"in vec4 colour;\n"
|
|
|
|
"out vec4 fragColour;\n"
|
|
|
|
"\n"
|
|
|
|
"void main(void)\n"
|
|
|
|
"{\n"
|
|
|
|
"fragColour = colour;\n"
|
|
|
|
"}\n";
|
2015-07-17 00:40:46 +00:00
|
|
|
|
2015-07-24 00:07:35 +00:00
|
|
|
@interface CSAtari2600 (Callbacks)
|
|
|
|
- (void)crtDidEndFrame:(Outputs::CRTFrame *)frame;
|
|
|
|
@end
|
|
|
|
|
|
|
|
struct Atari2600CRTDelegate: public Outputs::CRT::CRTDelegate {
|
|
|
|
CSAtari2600 *atari;
|
|
|
|
void crt_did_end_frame(Outputs::CRT *crt, Outputs::CRTFrame *frame) { [atari crtDidEndFrame:frame]; }
|
|
|
|
};
|
|
|
|
|
|
|
|
@implementation CSAtari2600 {
|
|
|
|
Atari2600::Machine _atari2600;
|
|
|
|
Atari2600CRTDelegate _crtDelegate;
|
|
|
|
|
|
|
|
dispatch_queue_t _serialDispatchQueue;
|
2015-07-24 00:45:07 +00:00
|
|
|
Outputs::CRTFrame *_queuedFrame;
|
2015-07-25 00:37:08 +00:00
|
|
|
|
|
|
|
GLuint _vertexShader, _fragmentShader;
|
|
|
|
GLuint _shaderProgram;
|
|
|
|
|
|
|
|
GLuint _arrayBuffer, _vertexArray;
|
2015-07-24 00:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)crtDidEndFrame:(Outputs::CRTFrame *)frame {
|
2015-07-23 00:45:10 +00:00
|
|
|
|
2015-07-24 00:16:48 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2015-07-24 00:45:07 +00:00
|
|
|
if(_queuedFrame) {
|
|
|
|
dispatch_async(_serialDispatchQueue, ^{
|
|
|
|
_atari2600.get_crt()->return_frame();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_queuedFrame = frame;
|
|
|
|
|
|
|
|
[self.delegate atari2600NeedsRedraw:self];
|
|
|
|
});
|
|
|
|
}
|
2015-07-22 22:15:18 +00:00
|
|
|
|
2015-07-25 00:37:08 +00:00
|
|
|
- (GLuint)compileShader:(const char *)source type:(GLenum)type
|
|
|
|
{
|
|
|
|
GLuint shader = glCreateShader(type);
|
|
|
|
glShaderSource(shader, 1, &source, NULL);
|
|
|
|
glCompileShader(shader);
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:45:07 +00:00
|
|
|
- (void)draw {
|
|
|
|
|
2015-07-25 00:37:08 +00:00
|
|
|
if(!_shaderProgram)
|
|
|
|
{
|
|
|
|
_shaderProgram = glCreateProgram();
|
|
|
|
_vertexShader = [self compileShader:vertexShader type:GL_VERTEX_SHADER];
|
|
|
|
_fragmentShader = [self compileShader:fragmentShader type:GL_FRAGMENT_SHADER];
|
|
|
|
|
|
|
|
glAttachShader(_shaderProgram, _vertexShader);
|
|
|
|
glAttachShader(_shaderProgram, _fragmentShader);
|
|
|
|
glLinkProgram(_shaderProgram);
|
|
|
|
|
|
|
|
glGenVertexArrays(1, &_vertexArray);
|
|
|
|
glBindVertexArray(_vertexArray);
|
|
|
|
glGenBuffers(1, &_arrayBuffer);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _arrayBuffer);
|
|
|
|
|
|
|
|
GLfloat vertices[] = {
|
|
|
|
0.0f, 0.0f,
|
|
|
|
0.5f, 0.0f,
|
|
|
|
0.5f, 0.6f,
|
|
|
|
0.0f, 0.5f
|
|
|
|
};
|
|
|
|
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:45:07 +00:00
|
|
|
if(_queuedFrame)
|
|
|
|
{
|
|
|
|
glClearColor(1.0, 0.0, 0.0, 1.0);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2015-07-25 00:37:08 +00:00
|
|
|
|
|
|
|
glUseProgram(_shaderProgram);
|
|
|
|
|
|
|
|
GLint position = glGetAttribLocation(_shaderProgram, "position");
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(position);
|
|
|
|
glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 0, (void *)0);
|
|
|
|
|
|
|
|
glDrawArrays(GL_LINES, 0, 4);
|
|
|
|
|
2015-07-24 00:45:07 +00:00
|
|
|
/* printf("\n\n===\n\n");
|
2015-07-24 00:16:48 +00:00
|
|
|
int c = 0;
|
2015-07-24 00:45:07 +00:00
|
|
|
for(int run = 0; run < _queuedFrame->number_of_runs; run++)
|
2015-07-24 00:07:35 +00:00
|
|
|
{
|
2015-07-24 00:16:48 +00:00
|
|
|
char character = ' ';
|
2015-07-24 00:45:07 +00:00
|
|
|
switch(_queuedFrame->runs[run].type)
|
2015-07-24 00:16:48 +00:00
|
|
|
{
|
|
|
|
case Outputs::CRTRun::Type::Sync: character = '<'; break;
|
|
|
|
case Outputs::CRTRun::Type::Level: character = '_'; break;
|
|
|
|
case Outputs::CRTRun::Type::Data: character = '-'; break;
|
|
|
|
case Outputs::CRTRun::Type::Blank: character = ' '; break;
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:45:07 +00:00
|
|
|
if(_queuedFrame->runs[run].start_point.dst_x < 1.0 / 224.0)
|
2015-07-24 00:16:48 +00:00
|
|
|
{
|
2015-07-24 00:45:07 +00:00
|
|
|
printf("\n[%0.2f]: ", _queuedFrame->runs[run].start_point.dst_y);
|
2015-07-24 00:16:48 +00:00
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:45:07 +00:00
|
|
|
printf("(%0.2f): ", _queuedFrame->runs[run].start_point.dst_x);
|
|
|
|
float length = fabsf(_queuedFrame->runs[run].end_point.dst_x - _queuedFrame->runs[run].start_point.dst_x);
|
2015-07-24 00:16:48 +00:00
|
|
|
int iLength = (int)(length * 64.0);
|
|
|
|
for(int c = 0; c < iLength; c++)
|
|
|
|
{
|
|
|
|
putc(character, stdout);
|
|
|
|
}
|
2015-07-24 00:07:35 +00:00
|
|
|
}
|
2015-07-22 22:15:18 +00:00
|
|
|
|
2015-07-24 00:45:07 +00:00
|
|
|
printf("\n\n[%d]\n\n", c);*/
|
|
|
|
}
|
2015-07-17 00:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)runForNumberOfCycles:(int)cycles {
|
2015-07-24 00:07:35 +00:00
|
|
|
dispatch_async(_serialDispatchQueue, ^{
|
|
|
|
_atari2600.run_for_cycles(cycles);
|
|
|
|
});
|
2015-07-17 00:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setROM:(NSData *)rom {
|
2015-07-24 00:07:35 +00:00
|
|
|
dispatch_async(_serialDispatchQueue, ^{
|
|
|
|
_atari2600.set_rom(rom.length, (const uint8_t *)rom.bytes);
|
|
|
|
});
|
2015-07-17 00:40:46 +00:00
|
|
|
}
|
|
|
|
|
2015-07-22 22:15:18 +00:00
|
|
|
- (instancetype)init {
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (self) {
|
2015-07-24 00:07:35 +00:00
|
|
|
_crtDelegate.atari = self;
|
2015-07-22 22:15:18 +00:00
|
|
|
_atari2600.get_crt()->set_delegate(&_crtDelegate);
|
2015-07-24 00:16:48 +00:00
|
|
|
_serialDispatchQueue = dispatch_queue_create("Atari 2600 queue", DISPATCH_QUEUE_SERIAL);
|
2015-07-22 22:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-07-17 00:40:46 +00:00
|
|
|
@end
|