KoMo2  1.0.0
A modern ARM emulator GUI.
Classes | Enumerations | Functions | Variables
jimulatorInterface.cpp File Reference

Contains functionality relating to the serialization, transmission and reception of data to and from Jimulator, with a minor amount of processing done either way. More...

#include "jimulatorInterface.h"
#include <ctype.h>
#include <fcntl.h>
#include <gdk/gdkkeysyms.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/poll.h>
#include <sys/signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <regex>
#include <string>
#include <unordered_map>
#include <vector>
#include <sstream>

Go to the source code of this file.

Classes

class  BreakpointInfo
 Contains the information read from Jimulator about a given breakpoint. More...
 
class  SourceFileLine
 Describes a single line of a .kmd file. More...
 
class  sourceFile
 Describes an entire file of a .kmd sourceFile. More...
 

Enumerations

enum  BoardInstruction : unsigned char {
  START = 0xB0, WOT_U_DO = 0x20, STOP = 0x21, CONTINUE = 0x23,
  RESET = 0x04, FR_WRITE = 0x12, FR_READ = 0x13, BP_WRITE = 0x30,
  BP_READ = 0x31, BP_SET = 0x32, BP_GET = 0x33, GET_REG = 0x5A,
  SET_REG = 0x52, GET_MEM = 0x4A, SET_MEM = 0x40, START = 0xB0,
  WOT_U_DO = 0x20, STOP = 0x21, CONTINUE = 0x23, RESET = 0x04,
  FR_WRITE = 0x12, FR_READ = 0x13, BP_WRITE = 0x30, BP_READ = 0x31,
  BP_SET = 0x32, BP_GET = 0x33, GET_REG = 0x5A, SET_REG = 0x52,
  GET_MEM = 0x4A, SET_MEM = 0x40
}
 A container for a series of codes used as board instructions.
 

Functions

unsigned char operator| (BoardInstruction l, unsigned char r)
 Performing an or between a BoardInstruction and an unsigned char. More...
 
void flushSourceFile ()
 removes all of the old references to the previous file.
 
const bool readSourceFile (const char *const pathToKMD)
 Reads the source of the file pointer to by pathToKMD. More...
 
const ClientState getBoardStatus ()
 Gets a code that indicates the internal state of Jimulator. More...
 
const std::array< unsigned char, 64 > readRegistersIntoArray ()
 Gets serialized bit data from the board that represents 16 register values - 15 general purpose registers and the PC. More...
 
constexpr const int disassembleSourceFile (SourceFileLine *src, unsigned int addr)
 Calculates the difference between the current address to display and the next address that needs to be displayed. More...
 
constexpr const bool moveSrc (bool firstFlag, SourceFileLine **src)
 Steps any given source line to the next valid source line. More...
 
const std::string generateMemoryHex (SourceFileLine **src, const uint32_t s_address, int *const increment, const int currentAddressI, unsigned char(*memdata)[52])
 Get the hex values from the memory data. More...
 
void sendNBytes (int data, int n)
 Writes n bytes of data to Jimulator. More...
 
void sendChar (unsigned char data)
 Sends a singular character to Jimulator. More...
 
void sendCharArray (int length, unsigned char *data)
 Sends an array of characters to Jimulator. More...
 
const int getNBytes (int *data, int n)
 Reads n bytes of data from Jimulator. More...
 
const int getChar (unsigned char *data)
 Reads a singular character from Jimulator. More...
 
const int getCharArray (int length, unsigned char *data)
 reads an array of characters from Jimulator. More...
 
constexpr const int getNextFreeBreakpoint (const int v)
 Gets the index of the next free breakpoint from Jimulators internal breakpoint list. More...
 
const bool getBreakpointStatus (unsigned int *wordA, unsigned int *wordB)
 Gets the status of breakpoints from within Jimulator. More...
 
const bool getBreakpointDefinition (unsigned int breakpointlistIndex, BreakpointInfo *bp)
 Reads a breakpoint definition for use by the breakpoints callbacks. More...
 
void setBreakpointStatus (unsigned int wordA, unsigned int wordB)
 Overwrites some breakpoint information into Jimulator. More...
 
void setBreakpointDefinition (unsigned int breakpointIndex, BreakpointInfo *bp)
 Writes a new breakpoint into the breakpoint list. More...
 
const std::unordered_map< u_int32_t, bool > getAllBreakpoints ()
 Reads all of the breakpoints from Jimulator into a map that can be indexed by address. The address is the key, with a boolean as the value to form a pair. However the boolean is redundant - since the address is the key, if you lookup an address in the map and it is present, you know that a breakpoint was found there. More...
 
constexpr void copyStringLiterals (int i, unsigned char *source, unsigned char *dest)
 Copy one string literal into another string literal. More...
 
constexpr const int numericStringSubtraction (const unsigned char *const s1, const unsigned char *const s2)
 Takes two string literals of equal length which represent string forms of integers - for example, the number 5 represented as the string {'5'}, or the number 327 represented as the string {'3', '2', '7'} - and performs subtraction on the integer values they represent as strings, returning that value as an int. For example, s1 = "400", s2 = "350", the return value 50. More...
 
constexpr const int numericStringToInt (int i, const unsigned char *const s)
 Takes a string literals which represent a string form of an integer - for example, the number 5 represented as the string {'5'}, or the number 327 represented as the string {'3', '2', '7'} - and returns the value represented as an int. For example: Input string {'8', '1', '8'} will result in the output 818. More...
 
constexpr void numericStringAndIntAddition (unsigned char *const s, int n)
 Takes a string representation of an integer - for example, the string {'5' '7' '2'} representing the integer 572 - and an integer number, adding these two "numbers" together into a second string literal. For example: The string {'6' '2'} and the integer 6 will output the string {'6' '6'}. The output of the addition is stored in the string paramter. More...
 
const std::string integerArrayToHexString (int i, unsigned char *const v, const bool prepend0x)
 Converts an array of integers into a formatted hexadecimal string. More...
 
constexpr const char getLeastSignificantByte (const int val)
 Get the least significant byte out of an integer - if little endian, get the most signficant byte. More...
 
constexpr const int rotateRight1Byte (const int val)
 Rotates an integers bits to the right by 1 byte. More...
 
constexpr const int rotateLeft1Byte (const int val)
 Rotates an integers bits to the left by 1 byte. More...
 
constexpr const int checkHexCharacter (const char character)
 Check if the passed character represents a legal hex digit. More...
 
constexpr const int readNumberFromFile (FILE *const f, char *const c, unsigned int *const n)
 Reads a n from a string of text in the .kmd file. More...
 
constexpr const unsigned int boardTranslateMemsize (const int size)
 Converts a provided memory size into a Jimulator legal memory size. More...
 
void boardSetMemory (unsigned char *const address, unsigned char *const value, const int size)
 Sets a memory value of a given address to a new value. This code is LEGACY. It used to run with a check on board_version: however board_version always passed the check due to the certainty of the "hardware" run under the emulator; therefore the check has been removed. More...
 

Variables

constexpr int SOURCE_BYTE_COUNT = 4
 The maximum number of bytes that can be read from a source file.
 
constexpr int SOURCE_FIELD_COUNT = 4
 The number of fields that can be used in a source file.
 
constexpr int SOURCE_TEXT_LENGTH = 100
 The maximum length of a line in a source file.
 
constexpr int IN_POLL_TIMEOUT = 1000
 The maximum amount of time to wait after sending input to the pipes.
 
constexpr int OUT_POLL_TIMEOUT = 100
 The maximum amount of time to wait waiting for output from the pipes.
 
constexpr int ADDRESS_BUS_WIDTH = 4
 The width of Jimulators internal address bus.
 
constexpr int MAX_NUMBER_OF_BREAKPOINTS = 32
 The maximum number of breakpoints within the application.
 
sourceFile source
 The source file that is currently loaded into Jimulator.
 

Detailed Description

Contains functionality relating to the serialization, transmission and reception of data to and from Jimulator, with a minor amount of processing done either way.

Author
Lawrence Warren (lawre.nosp@m.ncew.nosp@m.arren.nosp@m.2@gm.nosp@m.ail.c.nosp@m.om)
Version
1.0.0
Date
10-04-2021

Definition in file jimulatorInterface.cpp.

Function Documentation

void boardSetMemory ( unsigned char *const  address,
unsigned char *const  value,
const int  size 
)

Sets a memory value of a given address to a new value. This code is LEGACY. It used to run with a check on board_version: however board_version always passed the check due to the certainty of the "hardware" run under the emulator; therefore the check has been removed.

Parameters
addresspointer to the address (in bytes)
valuepointer to the new value to be stored
sizewidth of current memory (in bytes)

Definition at line 1278 of file jimulatorInterface.cpp.

constexpr const unsigned int boardTranslateMemsize ( const int  size)

Converts a provided memory size into a Jimulator legal memory size.

Parameters
sizeThe size to convert.
Returns
unsigned int The Jimulator legal size.

Definition at line 1254 of file jimulatorInterface.cpp.

constexpr const int checkHexCharacter ( const char  character)

Check if the passed character represents a legal hex digit.

Parameters
characterthe character under test
Returns
int the hex value or -1 if not a legal hex digit

Definition at line 1196 of file jimulatorInterface.cpp.

constexpr void copyStringLiterals ( int  i,
unsigned char *  source,
unsigned char *  dest 
)

Copy one string literal into another string literal.

Parameters
iThe length of the string literals.
sourceThe value to be copied.
destThe location to copy the value into.

Definition at line 820 of file jimulatorInterface.cpp.

constexpr const int disassembleSourceFile ( SourceFileLine src,
unsigned int  addr 
)

Calculates the difference between the current address to display and the next address that needs to be displayed.

Parameters
srcThe current line in the source file.
addrThe current address pointed to.
Returns
const int The difference between the current address to display and the next address that needs to be displayed.

Definition at line 1109 of file jimulatorInterface.cpp.

const std::string generateMemoryHex ( SourceFileLine **  src,
const uint32_t  s_address,
int *const  increment,
const int  currentAddressI,
unsigned char(*)  memdata[52] 
)

Get the hex values from the memory data.

Warning
This function is very side-effecty. I would not changing it if you can help it.
Parameters
srcA pointer to the source line pointer.
s_addressThe starting address of the memory window (the address of the row at the top of the window).
incrementThe amount to add onto the current address to get the address of the next memory row.
currentAddressIThe current address of the current memory row.
memdataThe memory information fetched from Jimulator.
Returns
const std::string The hex string.

Definition at line 734 of file jimulatorInterface.cpp.

const std::unordered_map<u_int32_t, bool> getAllBreakpoints ( )

Reads all of the breakpoints from Jimulator into a map that can be indexed by address. The address is the key, with a boolean as the value to form a pair. However the boolean is redundant - since the address is the key, if you lookup an address in the map and it is present, you know that a breakpoint was found there.

Returns
const std::unordered_map<u_int32_t, bool> A map of addresses.

Definition at line 1143 of file jimulatorInterface.cpp.

const ClientState getBoardStatus ( )

Gets a code that indicates the internal state of Jimulator.

Returns
int A code indicating the internal state of Jimulator.

Definition at line 991 of file jimulatorInterface.cpp.

const bool getBreakpointDefinition ( unsigned int  breakpointlistIndex,
BreakpointInfo bp 
)

Reads a breakpoint definition for use by the breakpoints callbacks.

Parameters
breakpointlistIndexInternally within Jimulator, breakpoints are stored as an unordered list. This value the index within that list of breakpoints which should be read. breakpointlistIndex has a maximum value of 32, as there can only be 32 breakpoints.
bpThe breakpoint information read from Jimulator will be stored in this struct.
Returns
bool true if reading was a success, else false.

Definition at line 778 of file jimulatorInterface.cpp.

const bool getBreakpointStatus ( unsigned int *  wordA,
unsigned int *  wordB 
)

Gets the status of breakpoints from within Jimulator.

Warning
The internals of Jimulator as a binary are a mystery to me. I have managed to decipher what this function does through trial and error. However I cannot decipher why it works in a (somewhat) mysterious way. This function works in tandem with other functions related to setting and reading breakpoints, and I would avoid changing it without serious understanding of how else it is used and what it does first.
Parameters
wordAThe first chunk of read breakpoint information will be read into here. It reveals the number of breakpoints in an unusual pattern:
  • 0 for 0 breakpoints
  • 1 for 1 breakpoint
  • 3 for 2 breakpoints
  • 7 for 3 breakpoints
  • 15 for 4 breakpoints
  • 31 for 5 breakpoints
  • 63, 127 and so on...

The pattern here is, if the number of breakpoints is 'n', it will return a 32-bit integer with the least significant 'n' bits set:

  • 0b00000000000000000000000000000000 = 0 breakpoints set = 0d0
  • 0b00000000000000000000000000000001 = 1 breakpoint set = 0d1
  • 0b00000000000000000000000000000011 = 2 breakpoints set = 0d3
  • 0b00000000000000000000000000000111 = 3 breakpoints set = 0d7
  • 0b00000000000000000000000000001111 = 4 breakpoints set = 0d15

This is why there can only be 32 breakpoints total.

AGAIN, why this is the way Jimulator works is a mystery.

Parameters
wordBThe second chunk of read breakpoint information will be read into here. Appears to always returns the largest 32-bit integer value (0xFFFFFFFF)
Returns
true If reading for breakpoints was a success.
false If reading for breakpoints was a failure.

Definition at line 691 of file jimulatorInterface.cpp.

const int getChar ( unsigned char *  data)

Reads a singular character from Jimulator.

Parameters
dataA pointer to a memory location where the read data can be stored.
Returns
int The number of bytes successfully received - either 1 or 0.

Definition at line 958 of file jimulatorInterface.cpp.

const int getCharArray ( int  length,
unsigned char *  data 
)

reads an array of characters from Jimulator.

Parameters
lengthThe number of characters to read from Jimulator.
dataA pointer to where to store the data rea from Jimulator.
Returns
int The number of bytes successfully received, up to length number of characters.

Definition at line 915 of file jimulatorInterface.cpp.

constexpr const char getLeastSignificantByte ( const int  val)

Get the least significant byte out of an integer - if little endian, get the most signficant byte.

Parameters
valThe integer to get the least significant byte of.
Returns
const char The least significant byte.

Definition at line 634 of file jimulatorInterface.cpp.

const int getNBytes ( int *  data,
int  n 
)

Reads n bytes of data from Jimulator.

Warning
This function reads data in a little-endian manner - that is, the least significant bit of data is on the left side of the array. Most systems use a big-endian architecture, so this may require conversion.
Parameters
dataA pointer to a location where the data can be stored.
nThe number of bytes to read.
Returns
int The number of bytes received successfully.

Definition at line 971 of file jimulatorInterface.cpp.

constexpr const int getNextFreeBreakpoint ( const int  v)

Gets the index of the next free breakpoint from Jimulators internal breakpoint list.

Parameters
vIndicates which breakpoints are set, as an array of bits.
Returns
const int The index of the next free breakpoint.

Definition at line 620 of file jimulatorInterface.cpp.

const std::string integerArrayToHexString ( int  i,
unsigned char *const  v,
const bool  prepend0x 
)

Converts an array of integers into a formatted hexadecimal string.

Warning
Jimulator often treats arrays of characters as plain arrays of bits to be manipulated. In this instance, although it is an array of characters, the paramter v is actually an array of integers.
Parameters
iThe number of bits to read.
vA pointer to the array of integers.
prepend0xWhether to prepend the output string with an "0x" or not.
Returns
std::string A hexadecimal formatted register value.

Definition at line 1041 of file jimulatorInterface.cpp.

constexpr const bool moveSrc ( bool  firstFlag,
SourceFileLine **  src 
)

Steps any given source line to the next valid source line.

Parameters
firstFlagIf this is the first time the source file has an error (?)
srcA pointer to the source line pointer.
Returns
bool true if the firstFlag is set.

Definition at line 704 of file jimulatorInterface.cpp.

constexpr void numericStringAndIntAddition ( unsigned char *const  s,
int  n 
)

Takes a string representation of an integer - for example, the string {'5' '7' '2'} representing the integer 572 - and an integer number, adding these two "numbers" together into a second string literal. For example: The string {'6' '2'} and the integer 6 will output the string {'6' '6'}. The output of the addition is stored in the string paramter.

Parameters
sThe string representation of a number, and also where the output of the addition is stored.
nThe integer to add.

Definition at line 1088 of file jimulatorInterface.cpp.

constexpr const int numericStringSubtraction ( const unsigned char *const  s1,
const unsigned char *const  s2 
)

Takes two string literals of equal length which represent string forms of integers - for example, the number 5 represented as the string {'5'}, or the number 327 represented as the string {'3', '2', '7'} - and performs subtraction on the integer values they represent as strings, returning that value as an int. For example, s1 = "400", s2 = "350", the return value 50.

Parameters
s1The "number" to have its value subtracted from.
s2The "number" to subtract by.
Returns
int The output of the arithmetic.

Definition at line 804 of file jimulatorInterface.cpp.

constexpr const int numericStringToInt ( int  i,
const unsigned char *const  s 
)

Takes a string literals which represent a string form of an integer - for example, the number 5 represented as the string {'5'}, or the number 327 represented as the string {'3', '2', '7'} - and returns the value represented as an int. For example: Input string {'8', '1', '8'} will result in the output 818.

Parameters
iThe length of the string.
sThe string representation of the number to have its value read.
Returns
int The value read from the string.

Definition at line 1068 of file jimulatorInterface.cpp.

unsigned char operator| ( BoardInstruction  l,
unsigned char  r 
)

Performing an or between a BoardInstruction and an unsigned char.

Parameters
lThe left hand BoardInstruction value.
rThe right hand unsigned char value.
Returns
unsigned char The result of the or operation.

Definition at line 147 of file jimulatorInterface.cpp.

constexpr const int readNumberFromFile ( FILE *const  f,
char *const  c,
unsigned int *const  n 
)

Reads a n from a string of text in the .kmd file.

Parameters
fA file handle.
cA pointer to the current character being read in the file.
nA pointer for where to read the found n into.
Returns
int The read n.

Definition at line 1215 of file jimulatorInterface.cpp.

const std::array<unsigned char, 64> readRegistersIntoArray ( )

Gets serialized bit data from the board that represents 16 register values - 15 general purpose registers and the PC.

Returns
const std::array<unsigned char, 64> An array of bytes fetched from Jimulator representing the memory values.

Definition at line 1018 of file jimulatorInterface.cpp.

const bool readSourceFile ( const char *const  pathToKMD)

Reads the source of the file pointer to by pathToKMD.

Parameters
pathToKMDA path to the .kmd file to be loaded.
Returns
true if successful, false otherwise.

Definition at line 1292 of file jimulatorInterface.cpp.

constexpr const int rotateLeft1Byte ( const int  val)

Rotates an integers bits to the left by 1 byte.

Parameters
valThe integer to rotate.
Returns
const int The rotated integer.

Definition at line 652 of file jimulatorInterface.cpp.

constexpr const int rotateRight1Byte ( const int  val)

Rotates an integers bits to the right by 1 byte.

Parameters
valThe integer to rotate.
Returns
const int The rotated integer.

Definition at line 643 of file jimulatorInterface.cpp.

void sendChar ( unsigned char  data)

Sends a singular character to Jimulator.

Parameters
dataThe character to send.

Definition at line 883 of file jimulatorInterface.cpp.

void sendCharArray ( int  length,
unsigned char *  data 
)

Sends an array of characters to Jimulator.

Parameters
lengthThe number of bytes to send from data.
dataAn pointer to the data that should be sent.

Definition at line 863 of file jimulatorInterface.cpp.

void sendNBytes ( int  data,
int  n 
)

Writes n bytes of data to Jimulator.

Parameters
dataThe data to be written.
nThe number of bytes to write.

Definition at line 892 of file jimulatorInterface.cpp.

void setBreakpointDefinition ( unsigned int  breakpointIndex,
BreakpointInfo bp 
)

Writes a new breakpoint into the breakpoint list.

Parameters
breakpointIndexThe index that this breakpoint will exist in within Jimulators internal list of breakpoint.
bpA struct containing all of the information about the new breakpoint.

Definition at line 847 of file jimulatorInterface.cpp.

void setBreakpointStatus ( unsigned int  wordA,
unsigned int  wordB 
)

Overwrites some breakpoint information into Jimulator.

Parameters
wordAThe value to set the breakpoint to. POSSIBLY represents whether the breakpoint is active? (e.g. 0 for inactive)
wordBThe index that the breakpoint exists within the list.

Definition at line 834 of file jimulatorInterface.cpp.