-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMemory.py
More file actions
78 lines (58 loc) · 2.7 KB
/
Memory.py
File metadata and controls
78 lines (58 loc) · 2.7 KB
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
import psycopg2, psycopg2.extras
def Memory(address ,data, read , write , Type) :
host_name = "185.105.239.28"
database_name = "camputerArchitecture"
root_user = "aliiiw"
root_password = "qazwsx"
port_id = 5432
connection_to_database = psycopg2.connect(
host=host_name,
dbname=database_name,
user=root_user,
password=root_password,
port=port_id)
open_connection = connection_to_database.cursor(cursor_factory=psycopg2.extras.DictCursor)
signed = bool(int(Type[2]))
size = [4 , 4 , 2 , 1][int(Type[:2], 2)]
address = int(address, 2)
if address % size != 0 :
address = size * (address // size)
if read == '1' :
print("read address: " , address)
Memdata = ""
value = size - 1
sql_select_script = "select * from datamemory where address between %s and %s;"
select_value = ( address, address + value)
open_connection.execute(sql_select_script, select_value)
result = open_connection.fetchall()
for element in result:
Memdata += element['string_data']
if signed :
Memdata = (32-len(Memdata)) * Memdata[0] + Memdata
else :
Memdata = (32-len(Memdata)) * '0' + Memdata
return Memdata
if write == '1':
print("write: " , address)
value = size - 1
sql_select_script = "select * from datamemory where address between %s and %s;"
select_value = ( address, address + value)
open_connection.execute(sql_select_script, select_value)
result = open_connection.fetchall()
for i in range(size) :
isUpdating = False
for res in result :
if res['address'] == address + i:
sql_update_script = "update datamemory set string_data = %s where address = %s"
update_value = (data[24 - (i * 8 ): 32 - (i * 8) ], address + size - 1 - i)
open_connection.execute(sql_update_script, update_value)
isUpdating = True
connection_to_database.commit()
break
if not isUpdating:
sql_insert_script = "insert into datamemory (address, string_data) values(%s, %s);"
insert_value = (address + size - 1 - i, data[24 - (i * 8) : 32 - (i * 8) ])
open_connection.execute(sql_insert_script, insert_value)
connection_to_database.commit()
connection_to_database.commit()
open_connection.close()