I
Immortal Nephi
I have seen many websites. They always state. Most programmers want
high performance. They are tired of writing OOP with C++ class. They
complain about CPU’s overheads because indirection has to be done to
modify data members. They want to use direct memory instead. They
decide to stick with old habit of writing C source code to use global
variables and global functions.
Set and Get member functions are very important because you want to
validate data members to avoid incorrect value. Sometimes, they use
if keyword. Sometimes, they find a way to modify data members without
needing to use if keyword.
If Set and Get member functions are not used, then you have to
examine each hundreds of member functions and try to find why data
member contains incorrect value. To reduce debugging is better
software engineering.
Let me give you my example class code with validation. You may
notice incomplete code such as NULL on data member—ram and
p_opcode_array. You do not need to say something about incomplete
code, but you know what I mean.
Tell me what you think if class design is to be good software
engineering. Compare opcode_1() and opcode_1_bad(). The execution on
this program can be overhead on the CPU. How can you find a way to
use direct memory instead of indirection? Inline keyword and static
keyword are the answer.
Tell me your opinion. Why do programmers want to avoid C++ class if
they want to write emulator with high performance?
class CPU
{
public:
CPU() : reg_a(0), reg_x(0), reg_y(0), reg_pc(0), reg_opcode(0), ram
(0) {}
~CPU() {}
void run()
{
(*p_opcode_array[reg_opcode])(); // run opcode_1(), ...,, opcode_256
()
}
private:
void opcode_1() // Good Software Engineering Practice
{
set_reg_a( ram[reg_pc] );
increment_reg_pc();
}
void opcode_1_bad() // Modifying data member directly is bad practice
{
reg_a = ram[reg_pc];
reg_a &= 0xFF;
reg_pc++;
reg_pc &= 0xFFFF;
}
void set_reg_a( unsigned reg)
{
reg_a = reg & 0xFF; // Validate to 8 bits only
}
unsigned int get_reg_a()
{
return reg_a;
}
void increment_reg_pc()
{
reg_pc++;
reg_pc &= 0xFFFF;
}
unsigned int reg_a;
unsigned int reg_x;
unsigned int reg_y;
unsigned int reg_pc;
unsigned int reg_opcode;
unsigned char* ram;
void (*p_opcode_array[0x100])();
};
high performance. They are tired of writing OOP with C++ class. They
complain about CPU’s overheads because indirection has to be done to
modify data members. They want to use direct memory instead. They
decide to stick with old habit of writing C source code to use global
variables and global functions.
Set and Get member functions are very important because you want to
validate data members to avoid incorrect value. Sometimes, they use
if keyword. Sometimes, they find a way to modify data members without
needing to use if keyword.
If Set and Get member functions are not used, then you have to
examine each hundreds of member functions and try to find why data
member contains incorrect value. To reduce debugging is better
software engineering.
Let me give you my example class code with validation. You may
notice incomplete code such as NULL on data member—ram and
p_opcode_array. You do not need to say something about incomplete
code, but you know what I mean.
Tell me what you think if class design is to be good software
engineering. Compare opcode_1() and opcode_1_bad(). The execution on
this program can be overhead on the CPU. How can you find a way to
use direct memory instead of indirection? Inline keyword and static
keyword are the answer.
Tell me your opinion. Why do programmers want to avoid C++ class if
they want to write emulator with high performance?
class CPU
{
public:
CPU() : reg_a(0), reg_x(0), reg_y(0), reg_pc(0), reg_opcode(0), ram
(0) {}
~CPU() {}
void run()
{
(*p_opcode_array[reg_opcode])(); // run opcode_1(), ...,, opcode_256
()
}
private:
void opcode_1() // Good Software Engineering Practice
{
set_reg_a( ram[reg_pc] );
increment_reg_pc();
}
void opcode_1_bad() // Modifying data member directly is bad practice
{
reg_a = ram[reg_pc];
reg_a &= 0xFF;
reg_pc++;
reg_pc &= 0xFFFF;
}
void set_reg_a( unsigned reg)
{
reg_a = reg & 0xFF; // Validate to 8 bits only
}
unsigned int get_reg_a()
{
return reg_a;
}
void increment_reg_pc()
{
reg_pc++;
reg_pc &= 0xFFFF;
}
unsigned int reg_a;
unsigned int reg_x;
unsigned int reg_y;
unsigned int reg_pc;
unsigned int reg_opcode;
unsigned char* ram;
void (*p_opcode_array[0x100])();
};