1 #include "Vu1Vm.h"
2 #include "Ps2Const.h"
3 #include "Log.h"
4 #include "ee/Vpu.h"
5 
6 #define LOG_NAME "Vu1Vm"
7 
CVu1Vm()8 CVu1Vm::CVu1Vm()
9     : m_vu1(MEMORYMAP_ENDIAN_LSBF)
10     , m_vuMem1(new uint8[PS2::VUMEM1SIZE])
11     , m_microMem1(new uint8[PS2::MICROMEM1SIZE])
12     , m_status(PAUSED)
13     , m_maVu1(PS2::VUMEM1SIZE - 1)
14     , m_vpu1_TOP(0)
15     , m_vpu1_ITOP(0)
16 {
17 	//Vector Unit 1 context setup
18 	{
19 		m_vu1.m_executor = std::make_unique<CVuExecutor>(m_vu1, PS2::MICROMEM1SIZE);
20 
21 		m_vu1.m_pMemoryMap->InsertReadMap(0x00000000, 0x00003FFF, m_vuMem1, 0x00);
22 		m_vu1.m_pMemoryMap->InsertReadMap(0x00008000, 0x00008FFF, [&](uint32 address, uint32 value) { return Vu1IoPortReadHandler(address); }, 0x01);
23 
24 		m_vu1.m_pMemoryMap->InsertWriteMap(0x00000000, 0x00003FFF, m_vuMem1, 0x00);
25 		m_vu1.m_pMemoryMap->InsertWriteMap(0x00008000, 0x00008FFF, [&](uint32 address, uint32 value) { return Vu1IoPortWriteHandler(address, value); }, 0x01);
26 
27 		m_vu1.m_pMemoryMap->InsertInstructionMap(0x00000000, 0x00003FFF, m_microMem1, 0x01);
28 
29 		m_vu1.m_pArch = &m_maVu1;
30 		m_vu1.m_pAddrTranslator = CMIPS::TranslateAddress64;
31 
32 		m_vu1.m_vuMem = m_vuMem1;
33 	}
34 
35 	Reset();
36 }
37 
~CVu1Vm()38 CVu1Vm::~CVu1Vm()
39 {
40 	delete[] m_vuMem1;
41 	delete[] m_microMem1;
42 }
43 
Pause()44 void CVu1Vm::Pause()
45 {
46 }
47 
Resume()48 void CVu1Vm::Resume()
49 {
50 }
51 
Reset()52 void CVu1Vm::Reset()
53 {
54 	m_vu1.Reset();
55 	m_vu1.m_executor->Reset();
56 	memset(m_vuMem1, 0, PS2::VUMEM1SIZE);
57 	memset(m_microMem1, 0, PS2::MICROMEM1SIZE);
58 }
59 
GetStatus() const60 CVirtualMachine::STATUS CVu1Vm::GetStatus() const
61 {
62 	return m_status;
63 }
64 
StepVu1()65 void CVu1Vm::StepVu1()
66 {
67 	m_vu1.m_executor->Execute(1);
68 	OnMachineStateChange();
69 	OnRunningStateChange();
70 }
71 
GetVu1Context()72 CMIPS* CVu1Vm::GetVu1Context()
73 {
74 	return &m_vu1;
75 }
76 
GetMicroMem1()77 uint8* CVu1Vm::GetMicroMem1()
78 {
79 	return m_microMem1;
80 }
81 
GetVuMem1()82 uint8* CVu1Vm::GetVuMem1()
83 {
84 	return m_vuMem1;
85 }
86 
SetVpu1Top(uint32 vpu1Top)87 void CVu1Vm::SetVpu1Top(uint32 vpu1Top)
88 {
89 	m_vpu1_TOP = vpu1Top;
90 }
91 
SetVpu1Itop(uint32 vpu1Itop)92 void CVu1Vm::SetVpu1Itop(uint32 vpu1Itop)
93 {
94 	m_vpu1_ITOP = vpu1Itop;
95 }
96 
Vu1IoPortReadHandler(uint32 address)97 uint32 CVu1Vm::Vu1IoPortReadHandler(uint32 address)
98 {
99 	uint32 result = 0xCCCCCCCC;
100 	switch(address)
101 	{
102 	case CVpu::VU_ITOP:
103 		result = m_vpu1_ITOP;
104 		break;
105 	case CVpu::VU_TOP:
106 		result = m_vpu1_TOP;
107 		break;
108 	default:
109 		CLog::GetInstance().Print(LOG_NAME, "Read an unhandled VU1 IO port (0x%08X).\r\n", address);
110 		break;
111 	}
112 	return result;
113 }
114 
Vu1IoPortWriteHandler(uint32 address,uint32 value)115 uint32 CVu1Vm::Vu1IoPortWriteHandler(uint32 address, uint32 value)
116 {
117 	switch(address)
118 	{
119 	case CVpu::VU_XGKICK:
120 		break;
121 	default:
122 		CLog::GetInstance().Print(LOG_NAME, "Wrote an unhandled VU1 IO port (0x%08X, 0x%08X).\r\n",
123 		                          address, value);
124 		break;
125 	}
126 	return 0;
127 }
128