Skip to content

Commit

Permalink
Added ALU events
Browse files Browse the repository at this point in the history
Also, minor fixes:
- Fixed the register pointes on the memory view component.
- Fixed the horizontal overflow and text wrapping on the events log
  viewer component.
  • Loading branch information
parraman committed Dec 10, 2017
1 parent 38b695b commit 98d09c1
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 143 deletions.
100 changes: 98 additions & 2 deletions src/app/alu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { CPUStackPointerRegister, CPUStatusRegister, CPURegister,
CPURegisterIndex } from './cpuregs';

import { Utils } from './utils';
import { EventsLogService, SystemEvent } from './events-log.service';

export enum ALUErrorType {

DIVISION_BY_ZERO = 0
Expand Down Expand Up @@ -115,7 +118,7 @@ export interface ALUOperationParamsBitshiftOp {

}

export class ALUOperation {
export class ALUOperation implements SystemEvent {

public operationType: ALUOperationType;
public data: any;
Expand All @@ -127,6 +130,99 @@ export class ALUOperation {

}

toString(): string {

let ret, params;

switch (this.operationType) {
case ALUOperationType.ADD:
params = <ALUOperationParamsAddition>this.data;
ret = `ALU: 16-bits addition (0x${Utils.pad(params.summand1, 16, 4)} + 0x${Utils.pad(params.summand2, 16, 4)}) = 0x${Utils.pad(params.result, 16, 4)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.ADDB:
params = <ALUOperationParamsAddition>this.data;
ret = `ALU: 8-bits addition (0x${Utils.pad(params.summand1, 16, 2)} + 0x${Utils.pad(params.summand2, 16, 2)}) = 0x${Utils.pad(params.result, 16, 2)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.SUB:
params = <ALUOperationParamsSubstraction>this.data;
ret = `ALU: 16-bits substraction (0x${Utils.pad(params.minuend, 16, 4)} - 0x${Utils.pad(params.subtrahend, 16, 4)}) = 0x${Utils.pad(params.result, 16, 4)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.SUBB:
params = <ALUOperationParamsSubstraction>this.data;
ret = `ALU: 8-bits substraction (0x${Utils.pad(params.minuend, 16, 2)} - 0x${Utils.pad(params.subtrahend, 16, 2)}) = 0x${Utils.pad(params.result, 16, 4)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.MUL:
params = <ALUOperationParamsMultiplication>this.data;
ret = `ALU: 16-bits multiplication (0x${Utils.pad(params.multiplicand, 16, 4)} * 0x${Utils.pad(params.multiplier, 16, 4)}) = 0x${Utils.pad(params.result, 16, 4)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.MULB:
params = <ALUOperationParamsMultiplication>this.data;
ret = `ALU: 8-bits multiplication (0x${Utils.pad(params.multiplicand, 16, 2)} * 0x${Utils.pad(params.multiplier, 16, 2)}) = 0x${Utils.pad(params.result, 16, 2)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.DIV:
params = <ALUOperationParamsDivision>this.data;
ret = `ALU: 16-bits integer division (0x${Utils.pad(params.dividend, 16, 4)} * 0x${Utils.pad(params.divisor, 16, 4)}) = 0x${Utils.pad(params.result, 16, 4)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.DIVB:
params = <ALUOperationParamsDivision>this.data;
ret = `ALU: 8-bits integer division (0x${Utils.pad(params.dividend, 16, 2)} * 0x${Utils.pad(params.divisor, 16, 2)}) = 0x${Utils.pad(params.result, 16, 2)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.AND:
params = <ALUOperationParamsBitwiseOp>this.data;
ret = `ALU: 16-bits bitwise AND (0x${Utils.pad(params.operand1, 16, 4)} & 0x${Utils.pad(params.operand2, 16, 4)}) = 0x${Utils.pad(params.result, 16, 4)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.ANDB:
params = <ALUOperationParamsBitwiseOp>this.data;
ret = `ALU: 8-bits bitwise AND (0x${Utils.pad(params.operand1, 16, 2)} & 0x${Utils.pad(params.operand2, 16, 2)}) = 0x${Utils.pad(params.result, 16, 2)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.OR:
params = <ALUOperationParamsBitwiseOp>this.data;
ret = `ALU: 16-bits bitwise OR (0x${Utils.pad(params.operand1, 16, 4)} | 0x${Utils.pad(params.operand2, 16, 4)}) = 0x${Utils.pad(params.result, 16, 4)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.ORB:
params = <ALUOperationParamsBitwiseOp>this.data;
ret = `ALU: 8-bits bitwise OR (0x${Utils.pad(params.operand1, 16, 2)} | 0x${Utils.pad(params.operand2, 16, 2)}) = 0x${Utils.pad(params.result, 16, 2)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.XOR:
params = <ALUOperationParamsBitwiseOp>this.data;
ret = `ALU: 16-bits bitwise XOR (0x${Utils.pad(params.operand1, 16, 4)} ^ 0x${Utils.pad(params.operand2, 16, 4)}) = 0x${Utils.pad(params.result, 16, 4)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.XORB:
params = <ALUOperationParamsBitwiseOp>this.data;
ret = `ALU: 8-bits bitwise XOR (0x${Utils.pad(params.operand1, 16, 2)} ^ 0x${Utils.pad(params.operand2, 16, 2)}) = 0x${Utils.pad(params.result, 16, 2)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.NOT:
params = <ALUOperationParamsBitwiseNegation>this.data;
ret = `ALU: 16-bits bitwise NOT (~0x${Utils.pad(params.operand, 16, 4)}) = 0x${Utils.pad(params.result, 16, 4)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.NOTB:
params = <ALUOperationParamsBitwiseNegation>this.data;
ret = `ALU: 8-bits bitwise NOT (~0x${Utils.pad(params.operand, 16, 2)}) = 0x${Utils.pad(params.result, 16, 2)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.SHL:
params = <ALUOperationParamsBitshiftOp>this.data;
ret = `ALU: 16-bits bitshift left (0x${Utils.pad(params.operand, 16, 4)} << 0x${Utils.pad(params.places, 16, 4)}) = 0x${Utils.pad(params.result, 16, 4)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.SHLB:
params = <ALUOperationParamsBitshiftOp>this.data;
ret = `ALU: 8-bits bitshift left (0x${Utils.pad(params.operand, 16, 2)} << 0x${Utils.pad(params.places, 16, 2)}) = 0x${Utils.pad(params.result, 16, 2)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.SHR:
params = <ALUOperationParamsBitshiftOp>this.data;
ret = `ALU: 16-bits bitshift right (0x${Utils.pad(params.operand, 16, 4)} >> 0x${Utils.pad(params.places, 16, 4)}) = 0x${Utils.pad(params.result, 16, 4)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
case ALUOperationType.SHRB:
params = <ALUOperationParamsBitshiftOp>this.data;
ret = `ALU: 8-bits bitshift right (0x${Utils.pad(params.operand, 16, 2)} >> 0x${Utils.pad(params.places, 16, 2)}) = 0x${Utils.pad(params.result, 16, 2)}, C = ${params.carryFlag}, Z = ${params.zeroFlag}`;
break;
default:
break;
}

return ret;

}

}

type PublishALUOperation = (operation: ALUOperation) => void;
Expand Down Expand Up @@ -389,7 +485,7 @@ export class ArithmeticLogicUnit {
this.SR.carry = ret.carryFlag;
this.SR.zero = ret.zeroFlag;

this.publishSubstraction(ALUOperationType.ADDB,
this.publishSubstraction(ALUOperationType.SUBB,
minuend, subtrahend, ret.result, ret.carryFlag, ret.zeroFlag);

return ret.result;
Expand Down
3 changes: 2 additions & 1 deletion src/app/cpu.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class ControlUnitOperation implements SystemEvent {
break;
case ControlUnitOperationType.EXECUTE:
params = <CUOperationParamsExecute>this.data;
ret = `CU: Execute {0x${Utils.pad(params.opcode, 16, 2)}: ${OpCode[params.opcode]}}`
ret = `CU: Execute instruction {0x${Utils.pad(params.opcode, 16, 2)}: ${OpCode[params.opcode]}}`

if (params.operand1Type !== undefined) {

Expand Down Expand Up @@ -305,6 +305,7 @@ export class CPUService {
protected publishALUOperation(operation: ALUOperation) {

this.aluOperationSource.next(operation);
this.eventsLogService.log(operation);

}

Expand Down
2 changes: 1 addition & 1 deletion src/app/events-log-viewer/events-log-viewer.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h5 style="display: inline-block">Events log</h5>
</div>
<div #logLinesContainer class="list-group events-log-list" appPreventScroll>
<div #logLinesItems *ngFor="let i of logLines" class="list-group-item source-code event" [ngClass]="i.style">
<span>{{i.text}}</span>
{{i.text}}
</div>
</div>
<div class="small"><span class="event-enabler control-unit-event" [ngClass]="{'enabled': enableControlUnit === true, 'muted': isRunning === true || enableLogging === false}">Control Unit: </span>
Expand Down
8 changes: 8 additions & 0 deletions src/app/events-log-viewer/events-log-viewer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import {
AfterViewInit, Component, Input,
ViewChild, ViewChildren, ElementRef, QueryList
} from '@angular/core';

import { EventsLogService, LoggedEvent } from '../events-log.service';
import { MemoryOperation, MemoryOperationType } from '../memory.service';
import { ControlUnitOperation } from '../cpu.service';
import { ALUOperation } from '../alu';

class LogLine {

Expand Down Expand Up @@ -87,6 +89,12 @@ export class EventsLogViewerComponent implements AfterViewInit {
`control-unit-event`);
this.logLines.push(newLine);

} else if (loggedEvent.systemEvent instanceof ALUOperation && this.enableALU === true) {

const newLine = new LogLine(`${loggedEvent.time}: ${loggedEvent.systemEvent.toString()}`,
`alu-event`);
this.logLines.push(newLine);

}

}
Expand Down

0 comments on commit 98d09c1

Please sign in to comment.