Skip to content

Commit

Permalink
screen working in the gui debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
poechsel committed Sep 13, 2017
1 parent c1b9da5 commit eca5f86
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 43 deletions.
4 changes: 2 additions & 2 deletions gui/codeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CodeView::CodeView(QWidget *parent):
void CodeView::update() {
QStringList code;
for (int i = 0; i < 0x10000; ++i) {
code.append(QString::fromStdString(dissassemble(_machine->memory[i])));
code.append(QString("[") + QString::number(i) + QString("]") + QString::fromStdString(dissassemble(_machine->memory[i])));
}
this->clear();
this->addItems(code);
Expand All @@ -21,7 +21,7 @@ void CodeView::updateCode(int row) {
if (!_machine) return;
QString foo = QString::fromStdString(dissassemble(_machine->memory[row]));
if (row < this->count())
this->item(row)->setText(foo);
this->item(row)->setText(QString("[") + QString::number(row) + QString("]") + foo);
}

void CodeView::setMachine(Machine &machine) {
Expand Down
10 changes: 7 additions & 3 deletions gui/gui.pro
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

INCLUDEPATH += /usr/bin/include/
LIBS += /usr/local/lib/ -lSDL2 -lpthread
LIBS += -lSDL2 -lpthread

SOURCES += \
main.cpp \
Expand All @@ -33,7 +33,9 @@ SOURCES += \
../simulateur/debug.cpp \
codeview.cpp \
memoryview.cpp \
sdlwidget.cpp
sdlwidget.cpp \
../simulateur/screen.cpp \
../simulateur/simulateur.cpp


HEADERS += \
Expand All @@ -43,7 +45,9 @@ HEADERS += \
../simulateur/debug.h \
codeview.h \
memoryview.h \
sdlwidget.h
sdlwidget.h \
../simulateur/screen.h \
../simulateur/simulateur.h

FORMS += \
mainwindow.ui
49 changes: 47 additions & 2 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ MainWindow::MainWindow(QWidget *parent) :
QLineEdit *nom = new QLineEdit;

QLineEdit *prenom = new QLineEdit;

QLineEdit *age = new QLineEdit;


Expand All @@ -32,7 +31,32 @@ MainWindow::MainWindow(QWidget *parent) :
_code_view = new CodeView;
layout->addWidget(_code_view);

layout->addWidget(age);

_screen_view = new SDLWidget;

QVBoxLayout *layout_w_controls = new QVBoxLayout;
layout->addLayout(layout_w_controls);

QGroupBox* panel_control = new QGroupBox("Contrôles");
layout_w_controls->addWidget(panel_control);
QGridLayout* grid_control_layout = new QGridLayout;

QPushButton* button_single_step = new QPushButton("Single");
QPushButton* button_next_step = new QPushButton("Next");
QPushButton* button_breakpoint = new QPushButton("Break");
QPushButton* button_play = new QPushButton("Play");
QPushButton* button_stop = new QPushButton("Stop");
grid_control_layout->addWidget(button_single_step, 0, 0);
grid_control_layout->addWidget(button_next_step, 0, 1);
grid_control_layout->addWidget(button_breakpoint, 0, 2);
grid_control_layout->addWidget(button_play, 1, 0);
grid_control_layout->addWidget(button_stop, 1, 1);

panel_control->setLayout(grid_control_layout);

layout_w_controls->addWidget(_screen_view);

_screen_view->resize(120, 340);


zoneCentrale->setLayout(layout);
Expand Down Expand Up @@ -61,6 +85,27 @@ void MainWindow::open_file() {
_code_view->update();
_memory_view->update();
connect(_memory_view, SIGNAL(cellChanged(int,int)), _memory_view, SLOT(editCell(int,int)));

ClockTicks ct = clockticks_new();
Param param;
param.step_by_step = false;
param.debug_output = false;
param.fast_mode = true;
param.full_debug = false;
param.skip_call = false;
machine.clock_ticks = ct;

QtConcurrent::run(this, &MainWindow::simulate,param, machine);
}

void MainWindow::simulate(Param &param, Machine &machine) {
machine.pc = 0;
uword previous_pc = -1;
while (machine.pc != previous_pc) {
previous_pc = machine.pc;
evaluate(machine.memory[machine.pc], machine, param, (Screen*) _screen_view);
}
qDebug()<<"quit\n";
}

MainWindow::~MainWindow()
Expand Down
8 changes: 7 additions & 1 deletion gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
#include "../simulateur/utilities.h"
#include "../simulateur/debug.h"
#include "memoryview.h"
#include "sdlwidget.h"
#include "../simulateur/simulateur.h"
#include <QtConcurrent/QtConcurrent>
#include <QGroupBox>
#include <QPushButton>

namespace Ui {
class MainWindow;
Expand All @@ -23,12 +28,13 @@ class MainWindow : public QMainWindow
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

void simulate(Param &param, Machine &machine);
private:
QStringList _code;
QListWidget *_list_view;
CodeView *_code_view;
MemoryView *_memory_view;
SDLWidget *_screen_view;

private slots:
void open_file();
Expand Down
3 changes: 0 additions & 3 deletions gui/memoryview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ void MemoryView::setMachine(Machine &machine) {
}

void MemoryView::_updateRow(int row, int value) {
qDebug()<<"--> "<<value<<" "<<this<<" "<<row<<"\n";
auto *t = new QTableWidgetItem(QString::number(value));
qDebug()<<t<<"\n";
if (row < this->rowCount()) {
this->item(row, 0)->setText(QString::number(value));
this->item(row, 1)->setText("0x" + QString::number(value, 16).rightJustified(4, '0'));
Expand Down
45 changes: 35 additions & 10 deletions gui/sdlwidget.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
#include "sdlwidget.h"

SDLWidget::SDLWidget(QWidget *parent) : QWidget(parent)
SDLWidget::SDLWidget(QWidget *parent) :
QWidget(parent), Screen()

{
char windowid[64];
#ifdef Q_WS_WIN
_pixels = new QRgb[WIDTH*HEIGHT];
_image = QImage((uchar*)_pixels, WIDTH, HEIGHT, QImage::Format_ARGB32);
this->setMinimumHeight(HEIGHT);
this->setMinimumWidth(WIDTH);
this->resize(2*WIDTH, 2*HEIGHT);
for (int i = 0; i < WIDTH * HEIGHT; ++i)
_pixels[i] = 0xff00000;

this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
}

SDLWidget::~SDLWidget() {
delete[] _pixels;
}

void SDLWidget::paintEvent(QPaintEvent *event) {
QPainter painter(this);
qDebug()<<"paint event\n";
QImage image = QImage((uchar*)_pixels, WIDTH, HEIGHT, QImage::Format_ARGB32);

sprintf(windowid, "SDL_WINDOWID=0x%lx", reinterpret_cast<qlonglong>(winId()));
#elif defined Q_WS_X11
sprintf(windowid, "SDL_WINDOWID=0x%lx", winId());
#else
qFatal("Fatal: cast du winId() inconnu pour votre plate-forme; toute information est la bienvenue!");
#endif
SDL_putenv(windowid);
painter.drawImage(0, 0, image);
}

void SDLWidget::updateContent(uword *memory) {
qDebug()<<"drawing\n";
for (unsigned int i = MEM_SCREEN_BEGIN; i <= 0xFFFF; ++i) {
word pixel = (memory) ? memory[i] : 0;
uint32_t blue = pixel & ((1<<5)-1);
uint32_t green = (pixel>>5) & ((1<<5)-1);
uint32_t red = (pixel>>10) ;
_pixels[i - MEM_SCREEN_BEGIN] = ((red << (2+16)) + (green << (3+8)) + (blue << 3)) | 0xff000000;
}
this->update();
}

15 changes: 12 additions & 3 deletions gui/sdlwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@
#include <QObject>
#include <QWidget>
#include <SDL2/SDL.h>
#undef main
#include "../simulateur/screen.h"
#include <QDebug>

#include <QPainter>

class SDLWidget : public QWidget
#undef main
class SDLWidget : public QWidget, public Screen
{
Q_OBJECT
public:
explicit SDLWidget(QWidget *parent = nullptr);

~SDLWidget();
void updateContent(uword *memory);
void paintEvent(QPaintEvent *event);
private:
QRgb* _pixels;
QImage _image;
signals:

public slots:
Expand Down
12 changes: 6 additions & 6 deletions simulateur/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ SDLScreen::SDLScreen():
_refresh(false),
_force_quit(false)
{
create();
createsdl();
_thread = std::thread(&SDLScreen::action, this);
}

SDLScreen::~SDLScreen() {
this->_force_quit = true;
_thread.join();
close();
closesdl();
}

void SDLScreen::update(uword *memory) {
void SDLScreen::updateContent(uword *memory) {
_memory = memory;
_refresh = true;
while (_refresh) {};
}

void SDLScreen::create() {
void SDLScreen::createsdl() {
SDL_Init(SDL_INIT_VIDEO);
_window = SDL_CreateWindow("Asm", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH*2, HEIGHT*2, 0);
_renderer = SDL_CreateRenderer(_window, -1, 0);
_texture = SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, WIDTH, HEIGHT);
}

void SDLScreen::close() {
void SDLScreen::closesdl() {
if (_texture)
SDL_DestroyTexture(_texture);
_texture = 0;
Expand Down Expand Up @@ -79,6 +79,6 @@ void SDLScreen::action() {
}
}
_refresh = false;
close();
closesdl();
}

19 changes: 7 additions & 12 deletions simulateur/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,30 @@
class Screen {
public:
virtual ~Screen() {};
virtual void update(uword *memory) = 0;
virtual void updateContent(uword *memory) = 0;
};

class SDLScreen : public Screen {
public:
SDLScreen();
~SDLScreen();
virtual void update(uword *memory);
private:
virtual void updateContent(uword *memory);
protected:
void action();
void create();
void close();

virtual void createsdl();
virtual void closesdl();
SDL_Window *_window;
SDL_Renderer *_renderer;
SDL_Texture *_texture;

private:

volatile bool _refresh;
bool _force_quit;
uword *_memory;
std::thread _thread;

};

/* this function deals with the screen
* machine -> the vm
* force_quit -> to detect if we lust close the screen (or shut-down the simulator)
* refr -> if true we must refresh the screen and wait. Otherwise nothing to be done
*/
void simulate_screen(const Machine& machine, bool &force_quit, volatile bool &refr, bool activate_refresh_command = true);

#endif
2 changes: 1 addition & 1 deletion simulateur/simulateur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void evaluate(const uword opcode, Machine &machine, Param &param, Screen* screen
while (refresh) {};
*/
if (screen)
screen->update(machine.memory);
screen->updateContent(machine.memory);
};
machine.pc++;
break;
Expand Down

0 comments on commit eca5f86

Please sign in to comment.