Skip to content

Commit

Permalink
Moving over from local machine
Browse files Browse the repository at this point in the history
  • Loading branch information
SamWilk committed Dec 16, 2021
1 parent 498f584 commit 45378ae
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 11 deletions.
36 changes: 33 additions & 3 deletions SIC Simulator/Assembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace SIC_Simulator
{


// Assigned to Kris Wieben
class Instruction
Expand Down Expand Up @@ -97,9 +96,9 @@ public override string ToString()

class Assembler
{

private static readonly char[] InvalidSymbolCharacters = { ' ', '$', '!', '=', '+', '-', '(', ')', '@' };

HashSet<string> DeviceName = new HashSet<string>(); // new
public static bool IsInstrcution(string who) => Assembler.Instructions.ContainsKey(who);
public static bool IsDirective(string who) => Assembler.Directives.Contains(who);
public static bool IsNotSymbol(string who)
Expand Down Expand Up @@ -305,6 +304,12 @@ void pass_one()
if (Instructions.ContainsKey(instruction_line.OpCode))
{
memory_address += 3;

if (instruction_line.OpCode.Equals("WD") || instruction_line.OpCode.Equals("TD") || instruction_line.OpCode.Equals("RD") )
{
DeviceName.Add(instruction_line.Operand);

}
}
else if (instruction_line.OpCode.Equals("WORD"))
{
Expand Down Expand Up @@ -446,6 +451,11 @@ void pass_two()
setSkippedAddress(row);
int val = Int32.Parse(row.Operand) & 0xFFFFFF;
SICSource += String.Format("{0,6:X6}", val);

foreach(var x in DeviceName){
if(row.Symbol.Contains(x))
DeviceDetector(val,row);
}
}
else if (row.OpCode.Equals("BYTE"))
{
Expand All @@ -470,6 +480,10 @@ void pass_two()
{ // hex
String[] tmp = row.Operand.Split('\'');
SICSource += String.Format("{0}", tmp[1]);
foreach(var x in DeviceName){
if(row.Symbol.Contains(x))
DeviceDetector(Convert.ToInt32(tmp[1], 16),row);
}
}
}
else if (row.OpCode.Equals("RESB") || row.OpCode.Equals("RESW"))
Expand Down Expand Up @@ -537,6 +551,22 @@ void saveTRecord(int current_address)
memory_address = current_address;
line_counter = 0;
}

/// <summary>
/// Checks Whether Set Device ID Is Within Range During
/// </summary>
/// <param name="val">value of Device ID</param>
/// <param name="row">instruction object</param>
void DeviceDetector(int val, Instruction row)
{
if(val > 64 || val < 0)
{
output += String.Format("{0} {1} {2}\nLine {3}: Device ID {4} Does Not Exist", row.Symbol, row.OpCode, row.Operand, row.LineNumber, val);
MessageBox.Show(output);
_process = PROCESS.ERROR;
return;
}
}

//MessageBox.Show(ObjectCode);
}
Expand Down
28 changes: 21 additions & 7 deletions SIC Simulator/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ private async Task DeviceRefreshAsync()
await Task.Run(() => { });
for (int i = 0; i < this.lvDevices.Items.Count; i++)
{ // update the sub list views with data found in the device array
this.lvDevices.Items[i].SubItems[1].Text = this.SICVirtualMachine.Devices[i].GetWriteBufferASCIIByteString;
try
{
this.lvDevices.Items[i].SubItems[1].Text = this.SICVirtualMachine.Devices[i].GetWriteBufferASCIIByteString;
}catch(NullReferenceException)
{
this.lvDevices.Items[i].SubItems[1].Text = "";
}
}
}

Expand Down Expand Up @@ -451,6 +457,7 @@ private void tsmloadAndAssembleSICSourceFIle_Click(object sender, EventArgs e)
if (loadSICSourceFD.ShowDialog() == DialogResult.OK)
{
Assembler assembler = new Assembler(loadSICSourceFD.FileName);
this.SICVirtualMachine.getSICSource(assembler);

if ( !String.IsNullOrEmpty(assembler.ObjectCode) )
{
Expand Down Expand Up @@ -598,12 +605,19 @@ private void btnResetProgram_Click(object sender, EventArgs e)

private void btnThreeStep_Click(object sender, EventArgs e)
{
this.SICVirtualMachine.PerformStep();
this.RefreshCPUDisplays();
this.SICVirtualMachine.PerformStep();
this.RefreshCPUDisplays();
this.SICVirtualMachine.PerformStep();
this.RefreshCPUDisplays();
int i = 0;

while( this.SICVirtualMachine.PC >=0 && this.SICVirtualMachine.PC <=32767 && i<3 ) {
this.SICVirtualMachine.PerformStep();
this.RefreshCPUDisplays();
i++;
}
//this.SICVirtualMachine.PerformStep();
//this.RefreshCPUDisplays();
//this.SICVirtualMachine.PerformStep();
//this.RefreshCPUDisplays();
//this.SICVirtualMachine.PerformStep();
//this.RefreshCPUDisplays();
}

private void loadObjectFileToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down
104 changes: 103 additions & 1 deletion SIC Simulator/SIC_CPU.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace SIC_Simulator
{

[Serializable()]
class SIC_CPU
class SIC_CPU : ISerializable

{
public readonly static int NumDevices = 65;
public readonly static int DEVICE_ID_PADDING = 16;
public int CurrentProgramEndAddress = 0;
public int CurrentProgramStartAddress = 0;
public int PC = 0;
public int A = 0;
public int X = 0;
public int L = 0;
public int SW = 0;
public Assembler assembler = null;

public byte[] MemoryBytes;

Expand All @@ -31,6 +35,11 @@ public String MicrocodeSteps
get { return this.MicroSteps.ToString(); }
}

// need to review assembly source to discern between BYTE and WORD directives
public void getSICSource(Assembler assembler) {
this.assembler = assembler;
}


/// <summary>
/// Constructs a SIC VM (CPU and Memory)
Expand Down Expand Up @@ -879,6 +888,35 @@ public void ExecuteInstruction(int OpCode, int TA)
int DeviceNumberToRead;
DeviceNumberToRead = this.FetchWord(TA);

/* We do this check in case a device ID is stored using a BYTE constant that is greater than zero.
Valid IDs require at most a byte of storage. Thus, for an ID stored in a WORD, the 16
most significant bits will be all zeros, i.e. the DEVICE_ID_PADDING = 16. Because pass 2 validates
that a device ID complies with the specified bounds (0-64), the eight most significant bits will
only have nonzero values when the device ID is stored using the BYTE directive*/
if((DeviceNumberToRead >> DEVICE_ID_PADDING) > 0)
DeviceNumberToRead >>= DEVICE_ID_PADDING;

/* In case device ID is stored using a BYTE constant that is equal to zero. We need this additional check
because we can only safely ignore the 16 least significant bits of a device ID when the 8 most significant
bits are a nonzero value. Since we cannot make any assumptions about how the device ID is stored in this case,
we explicitly check the program source to determine if the device ID was stored using BYTE or WORD */
else if((DeviceNumberToRead >> DEVICE_ID_PADDING) == 0) {
List<Instruction> InstructionList = assembler.InstructionList;
for(int i=0; i<InstructionList.Count; i++) {
Instruction instruction = InstructionList[i];
if(TA == instruction.MemoryAddress) {
if(instruction.OpCode.Equals("BYTE") || instruction.OpCode.Equals("WORD")) {
if(instruction.OpCode.Equals("BYTE")) {
DeviceNumberToRead >>= DEVICE_ID_PADDING;
}
break;
}
else
continue;
}
}
}

// Set Device's Status Word to BUSY
this.Devices[DeviceNumberToRead].DeviceSW &= 0xFFFF3F;

Expand Down Expand Up @@ -1001,6 +1039,35 @@ public void ExecuteInstruction(int OpCode, int TA)
int DeviceNumberToWriteTo;
DeviceNumberToWriteTo = this.FetchWord(TA);

/* We do this check in case a device ID is stored using a BYTE constant that is greater than zero.
Valid IDs require at most a byte of storage. Thus, for an ID stored in a WORD, the 16
most significant bits will be all zeros, i.e. the DEVICE_ID_PADDING = 16. Because pass 2 validates
that a device ID complies with the specified bounds (0-64), the eight most significant bits will
only have nonzero values when the device ID is stored using the BYTE directive*/
if((DeviceNumberToWriteTo >> DEVICE_ID_PADDING) > 0)
DeviceNumberToWriteTo >>= DEVICE_ID_PADDING;

/* In case device ID is stored using a BYTE constant that is equal to zero. We need this additional check
because we can only safely ignore the 16 least significant bits of a device ID when the 8 most significant
bits are a nonzero value. Since we cannot make any assumptions about how the device ID is stored in this case,
we explicitly check the program source to determine if the device ID was stored using BYTE or WORD */
else if((DeviceNumberToWriteTo >> DEVICE_ID_PADDING) == 0) {
List<Instruction> InstructionList = assembler.InstructionList;
for(int i=0; i<InstructionList.Count; i++) {
Instruction instruction = InstructionList[i];
if(TA == instruction.MemoryAddress) {
if(instruction.OpCode.Equals("BYTE") || instruction.OpCode.Equals("WORD")) {
if(instruction.OpCode.Equals("BYTE")) {
DeviceNumberToWriteTo >>= DEVICE_ID_PADDING;
}
break;
}
else
continue;
}
}
}

// Set Device's Status Word to BUSY
this.Devices[DeviceNumberToWriteTo].DeviceSW &= 0xFFFF3F;

Expand Down Expand Up @@ -1065,7 +1132,42 @@ public void DecodeInstruction(int FullInstruction, ref int OpCode, ref int Targe
}
}

/// <summary>
/// Set Alternate Values For A Serialized Object
/// Stores And Formats In XML
/// </summary>
/// <param name="info"> SerilizationInfo object used to customize serilization behavior</param>
public void GetObjectData(SerializationInfo info, StreamingContext context)
{

info.AddValue("PC_Register",PC);
info.AddValue("A_Register",A);
info.AddValue("X_Register", X);
info.AddValue("L_Register", L);
info.AddValue("SW_Register", SW);
info.AddValue("MemoryBytes", MemoryBytes);
info.AddValue("MicroSteps", MicroSteps);
for(int i = 0; i < NumDevices; i++)
{
info.AddValue("DeviceString" + i,Devices[i].GetWriteBufferASCIIByteString);
}
}

/// <summary>
/// Constructor that is called during deserialization
/// Reconstructs object from SerializationInfo info
/// </summary>
/// <param name="info">SerilizationInfo object used to customize serilization behavior</param>
public SIC_CPU(SerializationInfo info, StreamingContext context)
{
PC = (int)info.GetValue("PC_Register", typeof(int));
A = (int)info.GetValue("A_Register", typeof(int));
X = (int)info.GetValue("X_Register", typeof(int));
L = (int)info.GetValue("L_Register", typeof(int));
SW = (int)info.GetValue("SW_Register", typeof(int));
MemoryBytes = (byte[])info.GetValue("MemoryBytes", typeof(byte[]));
MicroSteps = (StringBuilder)info.GetValue("MicroSteps", typeof(StringBuilder));
}

}

Expand Down

0 comments on commit 45378ae

Please sign in to comment.