-
Notifications
You must be signed in to change notification settings - Fork 1
/
alu.c
86 lines (72 loc) · 2.3 KB
/
alu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* LC-3 Simulator
* Final Project (Project #6)
* TCSS 372 - Computer Architecture
* Spring 2018
*
* ALU Module File
*
* This is a simulator of the LC-3 (Little Computer) machine using an
* object-oriented approach in C. The simulator includes all standard LC-3
* functionality based on the finite state machine approach and the corresponding
* opcode tables for the machine, with an additional push-pop stack feature utilized
* on the previously reserved (1101) opcode.
*
* Group Members:
* Michael Fulton
* Enoch Chan
* Logan Stafford
*
* Base Code Contributors:
* Sam Brendel
* Michael Josten
* Sam Anderson
* Tyler Schupack
*/
#include "global.h"
#include "slc3.h"
#include <stdlib.h>
typedef struct alu_t {
word_t sr1;
word_t sr2;
word_t result;
} alu_t, *alu_p;
/** Sets the ALU fields to default values */
void initialize_alu(alu_p);
/** Allocates and initializes a new ALU module */
alu_p alu_create() {
alu_p alu = calloc(1, sizeof(alu_t));
initialize_alu(alu);
return alu;
}
/** Reinitializes the ALU without reallocation */
void alu_reset(alu_p alu) { initialize_alu(alu); }
/** Deallocates the ALU */
void alu_destroy(alu_p alu) { free(alu); }
/** Takes a snapshot of the ALU data for debugging or display purposes */
const alu_snapshot_t alu_get_snapshot(alu_p alu) {
alu_snapshot_t snapshot;
snapshot.a = alu->sr1;
snapshot.b = alu->sr2;
snapshot.result = alu->result;
return snapshot;
}
/** Load ALU source register 1 */
void alu_load_sr1(alu_p alu, word_t data) { alu->sr1 = data; }
/** Load ALU source register 2 */
void alu_load_sr2(alu_p alu, word_t data) { alu->sr2 = data; }
/** Fetch the result from ALU */
word_t alu_fetch_result(alu_p alu) { return alu->result; }
/** Execute ADD operation on loaded SR1 and SR2 values */
void alu_add(alu_p alu) { alu->result = alu->sr1 + alu->sr2; }
/** Execute AND operation on loaded SR1 and SR2 values. Note: "and" is a protected keyword in C
* so we'll use "and_op" here */
void alu_and(alu_p alu) { alu->result = alu->sr1 & alu->sr2; }
/** Execute NOT operation on loaded SR1 value */
void alu_not(alu_p alu) { alu->result = ~alu->sr1; }
/** Sets the ALU fields to default values */
void initialize_alu(alu_p alu) {
alu->sr1 = 0;
alu->sr2 = 0;
alu->result = 0;
}