Skip to content

Commit

Permalink
New strict execution mode, if an instruction that is malformed is exe…
Browse files Browse the repository at this point in the history
…cuted then halt the processor. The motivation for this is to prevent from executing obvious data
  • Loading branch information
TricksterGuy committed Jan 29, 2018
1 parent b3e1f7f commit 5863f89
Show file tree
Hide file tree
Showing 21 changed files with 658 additions and 162 deletions.
2 changes: 2 additions & 0 deletions complx/AdvancedLoadDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ AdvancedLoadDialog::AdvancedLoadDialog(wxWindow* parent, const LoadingOptions& o
pcValue->SetValue(wxString::Format("x%04x", opts.pc));
trueTraps->SetValue(opts.true_traps);
interrupts->SetValue(opts.interrupts);
strictExecution->SetValue(opts.strict_execution);
}

LoadingOptions AdvancedLoadDialog::GetOptions()
Expand Down Expand Up @@ -68,6 +69,7 @@ LoadingOptions AdvancedLoadDialog::GetOptions()
options.pc = error != 0 ? 0x3000 : ret;
options.true_traps = trueTraps->IsChecked();
options.interrupts = interrupts->IsChecked();
options.strict_execution = strictExecution->IsChecked();
return options;
}

Expand Down
255 changes: 213 additions & 42 deletions complx/AdvancedLoadDialog.fbp

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion complx/AdvancedLoadDialogDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ AdvancedLoadDialogDecl::AdvancedLoadDialogDecl( wxWindow* parent, wxWindowID id,
bSizer7 = new wxBoxSizer( wxVERTICAL );

wxFlexGridSizer* fgSizer1;
fgSizer1 = new wxFlexGridSizer( 0, 2, 0, 0 );
fgSizer1 = new wxFlexGridSizer( 9, 2, 0, 0 );
fgSizer1->AddGrowableCol( 1 );
fgSizer1->SetFlexibleDirection( wxBOTH );
fgSizer1->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
Expand Down Expand Up @@ -126,6 +126,16 @@ AdvancedLoadDialogDecl::AdvancedLoadDialogDecl( wxWindow* parent, wxWindowID id,

fgSizer1->Add( interrupts, 0, wxTOP|wxBOTTOM|wxRIGHT, 5 );

m_staticText81 = new wxStaticText( m_panel2, wxID_ANY, _("Strict Execution"), wxDefaultPosition, wxDefaultSize, 0 );
m_staticText81->Wrap( -1 );
fgSizer1->Add( m_staticText81, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );

strictExecution = new wxCheckBox( m_panel2, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
strictExecution->SetValue(true);
strictExecution->SetToolTip( _("Enable processing of lc3 interrupts. This is automatically configued based on your assembly code.") );

fgSizer1->Add( strictExecution, 0, wxTOP|wxBOTTOM|wxRIGHT, 5 );


bSizer7->Add( fgSizer1, 1, wxEXPAND, 5 );

Expand Down
4 changes: 3 additions & 1 deletion complx/AdvancedLoadDialogDecl.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class AdvancedLoadDialogDecl : public wxDialog
wxCheckBox* trueTraps;
wxStaticText* m_staticText8;
wxCheckBox* interrupts;
wxStaticText* m_staticText81;
wxCheckBox* strictExecution;
wxButton* m_button1;
wxButton* m_button2;

Expand All @@ -66,7 +68,7 @@ class AdvancedLoadDialogDecl : public wxDialog

public:

AdvancedLoadDialogDecl( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = _("Advanced Load"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 500,360 ), long style = wxDEFAULT_DIALOG_STYLE );
AdvancedLoadDialogDecl( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = _("Advanced Load"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 499,393 ), long style = wxDEFAULT_DIALOG_STYLE );
~AdvancedLoadDialogDecl();

};
Expand Down
27 changes: 21 additions & 6 deletions complx/Complx.fbp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@
<event name="OnUpdateUI"></event>
</object>
</object>
<object class="wxMenu" expanded="0">
<object class="wxMenu" expanded="1">
<property name="label">State</property>
<property name="name">menuState</property>
<property name="permission">protected</property>
Expand Down Expand Up @@ -591,7 +591,7 @@
<property name="help">Enables True Traps</property>
<property name="id">ID_TRUE_TRAPS</property>
<property name="kind">wxITEM_CHECK</property>
<property name="label">True Traps</property>
<property name="label">&amp;True Traps</property>
<property name="name">menuStateTrueTraps</property>
<property name="permission">protected</property>
<property name="shortcut"></property>
Expand All @@ -606,16 +606,31 @@
<property name="help">Enables Interrupts</property>
<property name="id">ID_INTERRUPTS</property>
<property name="kind">wxITEM_CHECK</property>
<property name="label">Interrupts</property>
<property name="label">&amp;Interrupts</property>
<property name="name">menuStateInterrupts</property>
<property name="permission">protected</property>
<property name="shortcut"></property>
<property name="unchecked_bitmap"></property>
<event name="OnMenuSelection">OnInterrupts</event>
<event name="OnUpdateUI"></event>
</object>
<object class="separator" expanded="0">
<property name="name">m_separator3</property>
<object class="wxMenuItem" expanded="1">
<property name="bitmap"></property>
<property name="checked">1</property>
<property name="enabled">1</property>
<property name="help">Enables strict execution mode.</property>
<property name="id">ID_STRICT_EXECUTION_MODE</property>
<property name="kind">wxITEM_CHECK</property>
<property name="label">&amp;Strict Execution Mode</property>
<property name="name">menuStateStrictExecution</property>
<property name="permission">protected</property>
<property name="shortcut">Ctrl+S</property>
<property name="unchecked_bitmap"></property>
<event name="OnMenuSelection">OnStrictExecution</event>
<event name="OnUpdateUI"></event>
</object>
<object class="separator" expanded="1">
<property name="name">m_separator6</property>
<property name="permission">none</property>
</object>
<object class="wxMenuItem" expanded="0">
Expand Down Expand Up @@ -649,7 +664,7 @@
<event name="OnUpdateUI"></event>
</object>
</object>
<object class="wxMenu" expanded="1">
<object class="wxMenu" expanded="0">
<property name="label">Debug</property>
<property name="name">menuDebug</property>
<property name="permission">protected</property>
Expand Down
12 changes: 9 additions & 3 deletions complx/ComplxApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static const wxCmdLineEntryDesc cmd_descriptions[] =
wxCMD_LINE_VAL_NUMBER, wxCMD_LINE_PARAM_OPTIONAL
},
{
wxCMD_LINE_OPTION, "s", "stack_size", "Sets the undo stack size default 65536 instructions",
wxCMD_LINE_OPTION, "u", "undo_stack_size", "Sets the undo stack size default 65536 instructions",
wxCMD_LINE_VAL_NUMBER, wxCMD_LINE_PARAM_OPTIONAL
},
{
Expand All @@ -61,6 +61,10 @@ static const wxCmdLineEntryDesc cmd_descriptions[] =
wxCMD_LINE_OPTION, "r", "fill_registers", "Registers fill value default random",
wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL
},
{
wxCMD_LINE_OPTION, "s", "strict_execution", "Enable strict execution mode 0 = no 1 = yes (default)",
wxCMD_LINE_VAL_NUMBER, wxCMD_LINE_PARAM_OPTIONAL
},
{
wxCMD_LINE_OPTION, "m", "fill_memory", "Memory fill value default random",
wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL
Expand All @@ -73,7 +77,7 @@ static const wxCmdLineEntryDesc cmd_descriptions[] =
IMPLEMENT_APP(ComplxApp)

// Command line
long disassemble = 1, stack_size = 65536, call_stack_size = 10000, true_traps = 0, interrupts = 0, highlight = 1, pc = 0x3000, memory_fill = 0, register_fill = 0;
long disassemble = 1, stack_size = 65536, call_stack_size = 10000, true_traps = 0, interrupts = 0, highlight = 1, pc = 0x3000, memory_fill = 0, register_fill = 0, strict_execution = 1;
wxString address_str = wxEmptyString, register_fill_str = "random", memory_fill_str = "random";
wxArrayString files;
ComplxFrame* complxframe;
Expand Down Expand Up @@ -123,6 +127,7 @@ bool ComplxApp::OnInit()
opts.stack_size = stack_size;
opts.call_stack_size = call_stack_size;
opts.highlight = highlight != 0;
opts.loading_options.strict_execution = strict_execution != 0;
opts.width = 800;
opts.height = 600;

Expand Down Expand Up @@ -244,13 +249,14 @@ int ComplxApp::OnExit()
bool ComplxApp::OnCmdLineParsed(wxCmdLineParser& parser)
{
parser.Found(_("d"), &disassemble);
parser.Found(_("s"), &stack_size);
parser.Found(_("u"), &stack_size);
parser.Found(_("a"), &address_str);
parser.Found(_("r"), &register_fill_str);
parser.Found(_("m"), &memory_fill_str);
parser.Found(_("t"), &true_traps);
parser.Found(_("ie"), &interrupts);
parser.Found(_("i"), &highlight);
parser.Found(_("s"), &strict_execution);

register_fill_str.MakeLower();
memory_fill_str.MakeLower();
Expand Down
12 changes: 12 additions & 0 deletions complx/ComplxFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,11 @@ void ComplxFrame::DoLoadFile(const LoadingOptions& opts)
lc3_set_true_traps(state, opts.true_traps || tvt_modification);
state.interrupt_enabled = opts.interrupts || ivt_modification;
console->SetInput(opts.console_input);
state.strict_execution = opts.strict_execution;
// Update menus
menuStateTrueTraps->Check(opts.true_traps || tvt_modification);
menuStateInterrupts->Check(opts.interrupts || ivt_modification);
menuStateStrictExecution->Check(opts.strict_execution);

if (tvt_modification)
{
Expand Down Expand Up @@ -996,6 +998,16 @@ void ComplxFrame::OnInterrupts(wxCommandEvent& event)
UpdateMemory();
}

/** OnStrictExecution
*
* Toggles strict execution mode.
*/
void ComplxFrame::OnStrictExecution(wxCommandEvent& event)
{
state.strict_execution = ~state.strict_execution;
UpdateMemory();
}

/** OnUndoStack
*
* Sets the undo stack's size
Expand Down
1 change: 1 addition & 0 deletions complx/ComplxFrame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class ComplxFrame : public ComplxFrameDecl
void OnControlModeAdvanced(wxCommandEvent& event) override;
void OnTrueTraps(wxCommandEvent& event) override;
void OnInterrupts(wxCommandEvent& event) override;
void OnStrictExecution(wxCommandEvent& event) override;
void OnClearConsole(wxCommandEvent& event) override;
void OnClearConsoleInput(wxCommandEvent& event) override;

Expand Down
12 changes: 9 additions & 3 deletions complx/ComplxFrameDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,16 @@ ComplxFrameDecl::ComplxFrameDecl( wxWindow* parent, wxWindowID id, const wxStrin

menuState->AppendSeparator();

menuStateTrueTraps = new wxMenuItem( menuState, ID_TRUE_TRAPS, wxString( _("True Traps") ) , _("Enables True Traps"), wxITEM_CHECK );
menuStateTrueTraps = new wxMenuItem( menuState, ID_TRUE_TRAPS, wxString( _("&True Traps") ) , _("Enables True Traps"), wxITEM_CHECK );
menuState->Append( menuStateTrueTraps );

menuStateInterrupts = new wxMenuItem( menuState, ID_INTERRUPTS, wxString( _("Interrupts") ) , _("Enables Interrupts"), wxITEM_CHECK );
menuStateInterrupts = new wxMenuItem( menuState, ID_INTERRUPTS, wxString( _("&Interrupts") ) , _("Enables Interrupts"), wxITEM_CHECK );
menuState->Append( menuStateInterrupts );

menuStateStrictExecution = new wxMenuItem( menuState, ID_STRICT_EXECUTION_MODE, wxString( _("&Strict Execution Mode") ) + wxT('\t') + wxT("Ctrl+S"), _("Enables strict execution mode."), wxITEM_CHECK );
menuState->Append( menuStateStrictExecution );
menuStateStrictExecution->Check( true );

menuState->AppendSeparator();

wxMenuItem* menuStateClearConsole;
Expand Down Expand Up @@ -299,7 +303,7 @@ ComplxFrameDecl::ComplxFrameDecl( wxWindow* parent, wxWindowID id, const wxStrin
memory->SetDefaultCellAlignment( wxALIGN_LEFT, wxALIGN_CENTRE );
memory->SetFont( wxFont( wxNORMAL_FONT->GetPointSize(), wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false, wxEmptyString ) );

subsizer->Add( memory, 1, wxALL|wxEXPAND, 5 );
subsizer->Add( memory, 1, wxEXPAND, 5 );

controlSizer = new wxBoxSizer( wxHORIZONTAL );

Expand Down Expand Up @@ -646,6 +650,7 @@ ComplxFrameDecl::ComplxFrameDecl( wxWindow* parent, wxWindowID id, const wxStrin
this->Connect( menuStateControlModeAdvanced->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( ComplxFrameDecl::OnControlModeAdvanced ) );
this->Connect( menuStateTrueTraps->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( ComplxFrameDecl::OnTrueTraps ) );
this->Connect( menuStateInterrupts->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( ComplxFrameDecl::OnInterrupts ) );
this->Connect( menuStateStrictExecution->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( ComplxFrameDecl::OnStrictExecution ) );
this->Connect( menuStateClearConsole->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( ComplxFrameDecl::OnClearConsole ) );
this->Connect( menuStateClearConsoleInput->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( ComplxFrameDecl::OnClearConsoleInput ) );
this->Connect( menuDebugUndoStack->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( ComplxFrameDecl::OnUndoStack ) );
Expand Down Expand Up @@ -746,6 +751,7 @@ ComplxFrameDecl::~ComplxFrameDecl()
this->Disconnect( ID_ADVANCED, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( ComplxFrameDecl::OnControlModeAdvanced ) );
this->Disconnect( ID_TRUE_TRAPS, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( ComplxFrameDecl::OnTrueTraps ) );
this->Disconnect( ID_INTERRUPTS, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( ComplxFrameDecl::OnInterrupts ) );
this->Disconnect( ID_STRICT_EXECUTION_MODE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( ComplxFrameDecl::OnStrictExecution ) );
this->Disconnect( ID_CLEAR_CONSOLE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( ComplxFrameDecl::OnClearConsole ) );
this->Disconnect( wxID_ANY, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( ComplxFrameDecl::OnClearConsoleInput ) );
this->Disconnect( ID_UNDO_STACK, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( ComplxFrameDecl::OnUndoStack ) );
Expand Down
3 changes: 3 additions & 0 deletions complx/ComplxFrameDecl.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class ComplxFrameDecl : public wxFrame
ID_ADVANCED,
ID_TRUE_TRAPS,
ID_INTERRUPTS,
ID_STRICT_EXECUTION_MODE,
ID_CLEAR_CONSOLE,
ID_UNDO_STACK,
ID_CALL_STACK,
Expand Down Expand Up @@ -110,6 +111,7 @@ class ComplxFrameDecl : public wxFrame
wxMenu* menuStateControlMode;
wxMenuItem* menuStateTrueTraps;
wxMenuItem* menuStateInterrupts;
wxMenuItem* menuStateStrictExecution;
wxMenu* menuDebug;
wxMenu* menuHelp;
wxPanel* mainPanel;
Expand Down Expand Up @@ -163,6 +165,7 @@ class ComplxFrameDecl : public wxFrame
virtual void OnControlModeAdvanced( wxCommandEvent& event ) = 0;
virtual void OnTrueTraps( wxCommandEvent& event ) = 0;
virtual void OnInterrupts( wxCommandEvent& event ) = 0;
virtual void OnStrictExecution( wxCommandEvent& event ) = 0;
virtual void OnClearConsole( wxCommandEvent& event ) = 0;
virtual void OnClearConsoleInput( wxCommandEvent& event ) = 0;
virtual void OnUndoStack( wxCommandEvent& event ) = 0;
Expand Down
2 changes: 2 additions & 0 deletions complx/LoadingOptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ struct LoadingOptions
bool true_traps = false;
/** Override interrupts setting */
bool interrupts = false;
/** Override strict execution setting */
bool strict_execution = true;
};

#endif
7 changes: 1 addition & 6 deletions complx/MemoryView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,7 @@ wxString MemoryView::GetValue(int item, int column)
case MemoryInstruction:
// Change the pc temporarily...
state.pc = (unsigned short) (addr + 1);
if (disassemble_level == 0)
instruction = lc3_basic_disassemble(state, data);
else if (disassemble_level == 1)
instruction = lc3_disassemble(state, data);
else
instruction = lc3_smart_disassemble(state, data);
instruction = lc3_disassemble(state, data, 0);
ret = wxString::FromUTF8(instruction.c_str());
state.pc = pc;
break;
Expand Down
2 changes: 2 additions & 0 deletions lc3test/XmlTestParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ bool XmlTestParser::LoadTest(lc3_test_suite& suite, wxXmlNode* root)
test.randomize = wxAtoi(child->GetNodeContent()) != 0;
else if (child->GetName() == "fully-randomize")
test.fully_randomize = wxAtoi(child->GetNodeContent()) != 0;
else if (child->GetName() == "strict-execution")
test.strict_execution = wxAtoi(child->GetNodeContent()) != 0;
else if (child->GetName() == "random-seed")
{
std::string seed = child->GetNodeContent().ToStdString();
Expand Down
62 changes: 61 additions & 1 deletion liblc3/lc3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ std::string lc3_basic_disassemble(lc3_state& state, unsigned short data)
return buf;
}

std::string lc3_disassemble(lc3_state& state, unsigned short data)
std::string lc3_normal_disassemble(lc3_state& state, unsigned short data)
{
lc3_instr instr = lc3_decode(state, data);
unsigned int opcode = instr.data.opcode;
Expand Down Expand Up @@ -547,6 +547,66 @@ std::string lc3_smart_disassemble(lc3_state& state, unsigned short instruction)
return buf;
}

std::string lc3_disassemble(lc3_state& state, unsigned short data, int level)
{
std::string instr;
switch(level)
{
case 0:
instr = lc3_basic_disassemble(state, data);
break;
case 1:
instr = lc3_normal_disassemble(state, data);
break;
case 2:
instr = lc3_smart_disassemble(state, data);
break;
default:
instr = "";
break;
}

if (state.strict_execution)
{
switch((data >> 12) & 0xF)
{
case ADD_INSTR:
case AND_INSTR:
if ((data & 0x20) == 0 && (data & 0x18) != 0)
instr += " *";
break;
case BR_INSTR:
if ((data & 0xE0) == 0)
instr += " *";
break;
case JMP_INSTR:
if ((data & 0xE3F) != 0)
instr += " *";
break;
case JSRR_INSTR:
if ((data & 0x800) == 0 && (data & 0x63F) != 0)
instr += " *";
break;
case NOT_INSTR:
if ((data & 0x3F) != 0x3F)
instr += " *";
break;
case RTI_INSTR:
if ((data & 0xFFF) != 0)
instr += " *";
break;
case TRAP_INSTR:
if ((data & 0xF00) != 0)
instr += " *";
break;
default:
break;
}
}

return instr;
}

int lc3_load(lc3_state& state, std::istream& file, int (*reader)(std::istream&))
{
if (!file.good()) return -1;
Expand Down
Loading

0 comments on commit 5863f89

Please sign in to comment.