usr bin python coding utf-8 Laba import re from random import randrang

  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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Laba 6"""
import re
from random import randrange
class ProcessorEmulator():
"""Processor emulator class. To run just call run() method"""
def __init__(self):
self.mem_bits = 16
self.cells_count = 5
self.flags = {'PC':0, 'PF':0, 'TC':2, 'ZF':0, 'CF':0, 'SF':0}
self.reg = []
self.ram = []
self.acc = self.mask(randrange(70000))
for i in range(self.cells_count):
self.reg.append(self.mask(randrange(70000)))
self.ram.append(self.mask(randrange(70000)))
def mask(self, num):
"""Mask integer... You know.."""
return num & ((1<<self.mem_bits) - 1)
def to_bin(self, num):
"""Convert integer num to its binary representation"""
binary = bin(num & ((1<<self.mem_bits)-1))
binary = binary[2:] # delete '0b'
return binary.zfill(self.mem_bits)
def prepare(self, num):
"""Convert integer to unsigned integer?"""
if num < 0:
return (~(abs(num) - 1) + (1<<self.mem_bits)) & \
((1<<self.mem_bits+1) - 1)
else:
return num & ((1<<self.mem_bits+1) - 1)
def com_load(self, lst):
"""Parse and execute load command"""
operator = 0
if lst[0].isdigit():
operator = self.prepare(int(lst[0]))
elif lst[0][0] == '-' and lst[0][1:].isdigit():
operator = self.prepare(-1 * int(lst[0][1:]))
else:
match = re.match('^(r)([1-9])|(ram)([1-9])|(acc)$', lst[0])
try:
if match.group(1) == 'r':
operator = self.reg[int(match.group(2)) - 1]
elif match.group(3) == 'ram':
operator = self.ram[int(match.group(4)) - 1]
elif match.group(5): #accumulator
operator = self.acc
except AttributeError:
print('Wrong syntax')
return
#Set flags
if operator != operator & ((1<<self.mem_bits) - 1):
self.flags['CF'] = 1
if not operator:
self.flags['ZF'] = 1
if self.to_bin(operator)[0] == '1':
self.flags['SF'] = 1
#Load to accumulator
if len(lst) < 2:
self.acc = operator
return
match = re.match('^(r)([1-9])|(ram)([1-9])|(acc)$', lst[1])
try:
if match.group(1) == 'r':
self.reg[int(match.group(2)) - 1] = operator
elif match.group(3) == 'ram':
self.ram[int(match.group(4)) - 1] = operator
elif match.group(5): #accumulator
self.acc = operator
except AttributeError:
print('Wrong syntax')
return
def com_and(self, num):
"""Write to accumulator result of num & self.acc"""
operator = 0
if num.isdigit():
operator = self.prepare(int(num))
elif num[0] == '-' and num[1:].isdigit():
operator = self.prepare(-1 * int(num[1:]))
else:
match = re.match('^(r)([1-9])|(ram)([1-9])|(acc)$', num)
try:
if match.group(1) == 'r':
operator = self.reg[int(match.group(2)) - 1]
elif match.group(3) == 'ram':
operator = self.ram[int(match.group(4)) - 1]
elif match.group(5): #accumulator
operator = self.acc
except AttributeError:
print('Wrong syntax')
return
self.acc = self.acc & operator
if operator != operator & ((1<<self.mem_bits) - 1):
self.flags['CF'] = 1
if not self.to_bin(self.acc).count('1') % 2:
self.flags['PF'] = 1
if not self.acc:
self.flags['ZF'] = 1
if self.to_bin(self.acc)[0] == '1':
self.flags['SF'] = 1
def com_help(self):
"""Print help"""
print('Names: acc - accumulator; Ri - i`th register; ' +\
'RAMi - i`th register in RAM')
print('Commands:')
print(' ->"Load <a> [<b>]" ' + \
'-- load <a> (int, acc, Ri or RAMi) to <b> (acc, Ri or RAMi). '),
print('If no <b> specified - load <a> to accumulator.')
print(' ->"AND <a>" -- logical AND for accumulator and <a>.')
print('All results go to accumulator.')
print('Press return to show current status.')
print('Commands and names are case insensitive')
print('')
def run(self):
"""Main loop of emulator"""
line = raw_input('Command: ')
line = line.strip(' ')
line = line.lower()
while line != 'exit':
flag = 1
lst = re.split(' *', line)
if lst[0] == '':
self.print_stat()
flag = 0
elif lst[0] == 'and':
self.flags['PC'] = self.flags['PC'] + 1
self.flags['PF'] = 0
self.flags['CF'] = 0
self.flags['ZF'] = 0
self.flags['SF'] = 0
self.com_and(lst[1])
elif lst[0] == 'load' or lst[0] == 'l':
self.flags['PC'] = self.flags['PC'] + 1
self.flags['PF'] = 0
self.flags['CF'] = 0
self.flags['ZF'] = 0
self.flags['SF'] = 0
self.com_load(lst[1:])
elif lst[0] == 'help':
self.com_help()
flag = 0
else:
print('Wrong command')
flag = 0
if flag:
self.print_stat()
line = raw_input('Command: ')
line = line.strip(' ')
line = line.lower()
print('Good bye')
def print_stat(self):
"""Print accumulator, flags, all registers of processor and ram"""
print('Accumulator: ' + self.split_by_4(self.to_bin(self.acc)))
for i in range(self.cells_count):
print('R' + str(i + 1) + ': '),
print(self.split_by_4(self.to_bin(self.reg[i]))),
print(' '),
print('RAM' + str(i + 1) + ': '),
print(self.split_by_4(self.to_bin(self.ram[i])))
print('Flags: PC ='),
print(self.flags['PC']),
print(' TC ='),
print(self.flags['TC']),
print(' PF ='),
print(self.flags['PF']),
print(' ZF ='),
print(self.flags['ZF']),
print(' CF ='),
print(self.flags['CF']),
print(' SF ='),
print(self.flags['SF'])
print
def split_by_4(self, string):
"""Split string into block of 4 symbols"""
i = -4
while abs(i) < len(string):
string = string[:i] + ' ' + string[i:]
i = i - 5
return string
if __name__ == "__main__":
print('Welcome! The author is Kudlay Olexandr Lippo.net@gmail.com')
print
PR = ProcessorEmulator()
PR.run()