forked from timgabets/atm-opcode-buffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opcode.js
123 lines (109 loc) · 3.38 KB
/
opcode.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
function OperationCodeBufferService(log){
this.buffer = ' ';
/**
* [init description]
* @return {[type]} [description]
*/
this.init = function(){
this.buffer = ' ';
};
/**
* [set description]
* @param {[type]} buffer [description]
*/
this.set = function(buffer){
if(buffer.length !== 8)
return false;
this.buffer = buffer;
return true;
};
/**
* [getBuffer description]
* @return {[type]} [description]
*/
this.getBuffer = function(){
return this.buffer;
}
/**
* [setBufferValueAt set this.opcode_buffer[position] with the value ]
* @param {[type]} position [description]
* @param {[type]} value [description]
*/
this.setBufferValueAt = function(position, value){
if(position < 0 || position > 7){
log.error('Error changing Operation Code Buffer at ' + position + '. Must be non-negative integer less than 8');
return false;
}
if(value.length !== 1 ){
log.error('Error changing Operation Code Buffer to ' + value + '. Must be single-char string');
return false;
}
switch(value){
case ' ':
case 'A':
case 'B':
case 'C':
case 'D':
case 'F':
case 'G':
case 'H':
case 'I':
break;
default:
log.error('Error setting Operation Code Buffer: incorrect value: ' + value);
return false;
}
this.buffer = this.buffer.substr(0, position) + value + this.buffer.substr(position + 1);
return true;
};
/**
* [setBufferFromState process the D state logic (Pre‐Set Operation Code Buffer)]
* @param {[state]} state [D-type state]
* @param {[extension_state]} state [Z-type state]
*/
this.setBufferFromState = function(state, extension_state){
/**
* Specifies bytes of Operation Code buffer to be cleared to graphic ‘space’. Each bit relates to a byte
* in the Operation Code buffer. If a bit is zero, the corresponding entry is cleared. If a bit is one, the
* corresponding entry is unchanged.
*/
var mask = state.get('clear_mask');
for(var bit = 0; bit < 8; bit++)
{
if((mask & Math.pow(2, bit)).toString() === '0')
if(!this.setBufferValueAt(bit, ' '))
return false;
}
return true;
/**
* The buffer contains eight bytes. This entry sets the specified bytes to one of the values from keys[]. If a bit is one, the
* corresponding entry is set to keys[i]. If a bit is zero, the corresponding entry is unchanged.
*/
var keys = ['A', 'B', 'C', 'D'];
['A_preset_mask',
'B_preset_mask',
'C_preset_mask',
'D_preset_mask'
].forEach( (element, i) => {
mask = state[element];
for(var bit = 0; bit < 8; bit++){
if((mask & Math.pow(2, bit)).toString() === Math.pow(2, bit).toString())
if(!this.setBufferValueAt(bit, keys[i]))
return false;
}
});
if(extension_state && extension_state.entries){
var keys = [null, null, 'F', 'G', 'H', 'I'];
for(var i = 2; i < 6; i++){
mask = extension_state.entries[i];
for(var bit = 0; bit < 8; bit++){
if((mask & Math.pow(2, bit)).toString() === Math.pow(2, bit).toString())
if(!this.setBufferValueAt(bit, keys[i]))
return false;
}
};
}
return true;
}
}
module.exports = OperationCodeBufferService;