Actions

Difference between revisions of "Design of sxgo"

From Just in Time

m (62 revisions: copying content from old site)
 
(21 intermediate revisions by the same user not shown)
Line 1: Line 1:
This page describes the main design ideas behind [[sxgo]]. The implementation of this microcontroller emulator revolves around a few main ideas:
+
[[Sxgo]] constists of a core emulator library, a graphical UI and, alternatively, a set of Python bindings. This page describes the design of the core emulator. The implementation of this emulator revolves around a few main ideas:
  
 +
* a class that emulates the state of an SX, with member functions for each SX instruction.
 
* a meta-programmed list of instructions
 
* a meta-programmed list of instructions
 
* using meta programming to create a binary decision tree of those instructions for use in an instruction decoder.
 
* using meta programming to create a binary decision tree of those instructions for use in an instruction decoder.
Line 9: Line 10:
 
==Instruction Implementation==
 
==Instruction Implementation==
 
===implementation of the SX emulator===
 
===implementation of the SX emulator===
The idea is to have one class that consists of an SX 'state' (ram, rom, special registers, program counter) and that implements the instruction set. See the snippet (of file ''sx_controller.hpp'')below:
+
The idea is to have one class that consists of an SX 'state' (ram, rom, special registers, program counter) and that implements the instruction set. See the snippet (of file [https://github.com/DannyHavenith/sxsim/blob/master/sxsim/sx_controller.hpp sx_controller.hpp])below:
  
 
<source lang="cpp">
 
<source lang="cpp">
 +
 +
struct clr_fr      { /*...*/ };
 +
struct clr_w        { /*...*/ };
 +
struct mov_w_not_fr { /*...*/ };
 +
// etc...
 +
 
struct sx_controller_impl  
 
struct sx_controller_impl  
 
{
 
{
Line 36: Line 43:
 
</source>
 
</source>
  
As you can see, most instructions become very simple member functions and their implementation is quite readable. You may notice something odd: instead of naming member functions ''clr_frw'' or ''clr_w'', they're all called ''execute'' and take some--otherwise ignored--first argument of some type with names like ''clr_frw''. The reason for this is best explained after some explanation of the implementation of the [[#Instruction set|instruction set]] and the [[#Instruction decoder|instruction decoder]]. It has all to do with the fact that the instruction set is defined independently of it's implementaton and in fact there is more than one implementation of the instructions.
+
In C++ terms, this means that an implementation of the SX instruction set needs to ''model'' the [http://en.wikipedia.org/wiki/Concepts_%28C%2B%2B%29 Concept] of SXImplementation. Where (informally) the concept of SXImplementation is any class that implements one of the following member functions for each of the tags that are in the SX instruction set.
 +
<source lang="cpp">
 +
void execute( const tag &);
 +
void execute( const tag &, int arg);
 +
void execute( const tag &, int arg, int arg);
 +
</source>
 +
This concept is important, because as we shall see there are more models of this concept ('implementations of this interface') than just the sx emulator itself. We shall see that any class that models this concept can be used as the target of an instruction decoder, so that we can feed SX instruction words to a decoder and the decoder will call the correct overload of the ''execute'' function for such a class.
  
 
===Alternative instruction implementations===
 
===Alternative instruction implementations===
Line 47: Line 60:
 
As could be seen in the previous Section, the implementation of the sx instruction set was all in terms of an ''execute'' method that is overloaded for several types, each type representing one instruction. Defining the instruction set becomes therefore a matter of defining a set of types, one for each instruction. These are the ''instruction tag types'' and the execute method is similar to  a technique called ''[http://www.boost.org/community/generic_programming.html#tag_dispatching tag dispatching]''.
 
As could be seen in the previous Section, the implementation of the sx instruction set was all in terms of an ''execute'' method that is overloaded for several types, each type representing one instruction. Defining the instruction set becomes therefore a matter of defining a set of types, one for each instruction. These are the ''instruction tag types'' and the execute method is similar to  a technique called ''[http://www.boost.org/community/generic_programming.html#tag_dispatching tag dispatching]''.
  
In order to facilitate instruction decoding, the instruction types will be enhanced with information about their bit-patterns. This is in contrast with normal tag dispatching, where the tag types are completely empty types. The instruction list for sxgo can be found in ''sx_instruction_list.hpp'' and has the following form:
+
In order to facilitate instruction decoding, the instruction types will be enhanced with information about their bit-patterns. This is in contrast with normal tag dispatching, where the tag types are completely empty types. The instruction list for sxgo can be found in [https://github.com/DannyHavenith/sxsim/blob/master/sxsim/sx_instruction_list.hpp sx_instruction_list.hpp] and has the following form:
 
<source lang="cpp">
 
<source lang="cpp">
 
// ...
 
// ...
Line 88: Line 101:
  
 
==Instruction decoder==
 
==Instruction decoder==
As stated before, it is the task of the instruction decoder to take an instruction word (a number) and based on the value of that word, call an ''execute'' function with the right parameters. SX instructions are all 12 bits, and those bits are typically divided in a number of opcode bits (the instruction) and some operand bits (the arguments). The number of opcode bits is not the same for every instruction. The opcode for "''jmp''" for instance has only three bits (and they have the values '''''101'''''), the other nine bits are reserved for the 9-bit address to jump to.
+
An instruction decoder is essentially a function that takes an instruction word and a reference to an SXImplementation and that will determine from the instruction word which ''execute'' overload needs to be called and subsequently call that member function with the right arguments. In sxgo, this function is a static member function of a template class:
 +
 
 +
<source lang="cpp">
 +
    template<typename instruction_list, typename implementation_type>
 +
    struct instruction_decoder {
 +
        // ...
 +
        static void feed( instruction_type word, implementation_type &implementation);
 +
        // ...
 +
    };
 +
</source>
 +
 
 +
SX instructions are all 12 bits, and those bits are typically divided in a number of opcode bits (the instruction) and some operand bits (the arguments). The number of opcode bits is not the same for every instruction. The opcode for "''jmp''" for instance has only three bits (and they have the values '''''101'''''), the other nine bits are reserved for the 9-bit address to jump to.
  
 
There are several ways to go about implementing an instruction decoder. One could group all instructions according to opcode size and start checking for the shortest opcodes first, and then, if no opcode was recognized start looking in the next group of opcodes, etc. This would look somewhat like this:
 
There are several ways to go about implementing an instruction decoder. One could group all instructions according to opcode size and start checking for the shortest opcodes first, and then, if no opcode was recognized start looking in the next group of opcodes, etc. This would look somewhat like this:
 
<source lang="cpp">
 
<source lang="cpp">
void decode( int instruction, implementation &impl)
+
void feed( instruction_type instruction, implementation_type &impl)
 
{
 
{
 
     switch (instruction & 0xf00) // look at top 4 bits
 
     switch (instruction & 0xf00) // look at top 4 bits
Line 112: Line 136:
 
However, since I explicitly do not want to write the decoder by hand and case statements like the above one are hard to produce with template metaprogramming, I've chosen a slightly less efficient, but otherwise very similar approach: a binary decision tree. The implementation resembles the one above. Instead of creating nested case-statements, nested if-statements will test one bit at a time and as soon as a decision can be reached, the right member function will be called.
 
However, since I explicitly do not want to write the decoder by hand and case statements like the above one are hard to produce with template metaprogramming, I've chosen a slightly less efficient, but otherwise very similar approach: a binary decision tree. The implementation resembles the one above. Instead of creating nested case-statements, nested if-statements will test one bit at a time and as soon as a decision can be reached, the right member function will be called.
  
For example, suppose we have a simple microcontroller that uses three-bit instruction words and that nows only three instructions:
+
For example, suppose we have a simple microcontroller that uses three-bit instruction words and that knows only three instructions:
 
{| border="1"
 
{| border="1"
 
|-
 
|-
Line 130: Line 154:
 
Then a decision tree in the form of if-statements would look like this:
 
Then a decision tree in the form of if-statements would look like this:
 
<source lang="cpp">
 
<source lang="cpp">
void decode( int instruction, implementation &imp)
+
void feed( instruction_type instruction, implementation_type &imp)
 
{
 
{
 
     if (instruction & 4) // test bit 2
 
     if (instruction & 4) // test bit 2
Line 156: Line 180:
 
The algorithm recursively splits up the instruction set and works as follows:
 
The algorithm recursively splits up the instruction set and works as follows:
 
# start decoding at bit number ''d'' (discriminator) = 11 (the most significant bit) and the full instruction set
 
# start decoding at bit number ''d'' (discriminator) = 11 (the most significant bit) and the full instruction set
# for a given bit number, ''d'', partition the instruction set into those instructions that have a '1' at position ''d'', and those that have a '0'. This is done in ''instruction_decoder.hpp'' in the following lines:
+
# for a given bit number, ''d'', partition the instruction set into those instructions that have a '1' at position ''d'', and those that have a '0'. This is done in [https://github.com/DannyHavenith/sxsim/blob/master/sxsim/instruction_decoder.hpp instruction_decoder.hpp] in the following lines:
 
<source lang="cpp">
 
<source lang="cpp">
 
     // split the instructions into two sets: the ones that have a zero at bit 'discriminating_bit' and the
 
     // split the instructions into two sets: the ones that have a zero at bit 'discriminating_bit' and the
Line 172: Line 196:
 
   decide_node< 2, decide_node< 1, call_node<reset>, call_node<set> >, call_node< store> >;
 
   decide_node< 2, decide_node< 1, call_node<reset>, call_node<set> >, call_node< store> >;
 
</source>
 
</source>
[[File:Sx instruction decision tree.png|400px|right|thumb|Decision tree for the SX instruction decoder, [[Sx instruction decision tree|click here to see full image.]]]]
+
[[File:Sx instruction decision tree.png|400px|right|thumb|link=Sx instruction decision tree|Decision tree for the SX instruction decoder, click for larger image]]
  
 
So, after all of this, the instruction decoder has a single typedef, ''instruction_tree'' that defines a decision tree type that would look pretty much like the one above. Only for the full SX instruction set, the tree contains 53 instructions--and one BREAKPOINT instruction that I added myself--as. The typedef can be found near the bottom of file ''instruction_decoder.hpp'' and looks like this:
 
So, after all of this, the instruction decoder has a single typedef, ''instruction_tree'' that defines a decision tree type that would look pretty much like the one above. Only for the full SX instruction set, the tree contains 53 instructions--and one BREAKPOINT instruction that I added myself--as. The typedef can be found near the bottom of file ''instruction_decoder.hpp'' and looks like this:
Line 209: Line 233:
 
}
 
}
 
</source>
 
</source>
Of course there is an overload to end the recursion when a ''call_node'' is reached. This overload will finally call a member function that is supposed to imlement the instruction:
+
Of course there is an overload to end the recursion when a ''call_node'' is reached. This overload will finally call a member function that is supposed to implement the instruction:
 
<source lang="cpp">
 
<source lang="cpp">
 
//
 
//
Line 224: Line 248:
 
</source>
 
</source>
 
An optimizing compiler will inline most of these function calls, resulting in one big function, comprised of many if-statements.
 
An optimizing compiler will inline most of these function calls, resulting in one big function, comprised of many if-statements.
 +
 
==Precompilation==
 
==Precompilation==
 
The emulator spends a considerable amount of time in figuring out what instruction to execute, given the instruction word. It does this every time an instruction word at a given ROM location is executed, even though that instruction word will never change! It does make sense to try to do the instruction decoding once for every rom location before running the program.
 
The emulator spends a considerable amount of time in figuring out what instruction to execute, given the instruction word. It does this every time an instruction word at a given ROM location is executed, even though that instruction word will never change! It does make sense to try to do the instruction decoding once for every rom location before running the program.
Line 266: Line 291:
  
 
</source>
 
</source>
As you can see, an instruction is nothing more than a pointer to a function and two instruction arguments (the operands). Executing an instruction is nothing more than invoking the function pointer with the two stored arguments, ''a1'' and ''a2'' and a pointer to an ''implementation'' of the sx instruction set. The function ''f'' will then take care that the right member function is called on the implementation object.  
+
As you can see by looking at the data members of this struct, an instruction is nothing more than a pointer to a function and two instruction arguments (the operands). Executing an instruction is nothing more than invoking the function pointer with the two stored arguments, ''a1'' and ''a2'' and a pointer to an ''implementation'' of the sx instruction set. The function ''f'' will then take care that the right member function is called on the implementation object.  
  
At this point, you can probably imagine that there are function implementations for every instruction in the SX instruction set. Sxgo implements these by defining three templates: one for nullary instructions (having no arguments), one for instructions with one argument and one for instructions with two arguments. Below, you can see the implementation for instructions with one argument. This implementation will ignore the second argument call and pass the first argument to an implementation of an SX emulator.
+
At this point, you can probably imagine that there are function implementations for every instruction in the SX instruction set. Having to write those functions for every instruction would be quite cumbersome. Luckily, we can employ some template trickery to significantly limit the amount of work (as always). Sxgo implements the functions by defining only three template functions: one for nullary instructions (having no arguments), one for instructions with one argument and one for instructions with two arguments. These functions are implemented as static member functions of a template class. Below, you can see the implementation for instructions with one argument. This implementation will ignore the second argument call and pass the first argument to an implementation of an SX emulator.
  
 
The actual instruction that this object represents is determined by the ''tag'' template argument.
 
The actual instruction that this object represents is determined by the ''tag'' template argument.
Line 282: Line 307:
 
};
 
};
 
</source>
 
</source>
 +
 +
Note now, that the following pointer declaration:
 +
 +
<source lang="cpp">
 +
    &ins1<sx_emulator, clr_fr>::execute;
 +
</source>
 +
 +
is a pointer to a free function that takes two integer arguments and a pointer to an instance of the sx_emulator class, and that will call the ''clr_fr'' overload of the member function ''execute'' on the sx_emulator, with the first integer argument. Also note that this function pointer is of exactly the right type to be stored in the ''f'' data member of the ''compiled_instruction'' class...
  
 
Now that we have the ''compiled_instruction'' and the functions that these compiled instructions point to defined, it's time to show the full code of the compiler. Warning: don't blink, this will be a short one
 
Now that we have the ''compiled_instruction'' and the functions that these compiled instructions point to defined, it's time to show the full code of the compiler. Warning: don't blink, this will be a short one
Line 298: Line 331:
 
typedef compiled_instruction<implementation> slot_type;
 
typedef compiled_instruction<implementation> slot_type;
  
sx_compiler(
+
sx_compiler( slot_type &location)
slot_type &location_)
+
:location( location) // pun intended...
:location( location_)
 
 
{
 
{
 
}
 
}
Line 328: Line 360:
 
};
 
};
 
</source>
 
</source>
That's it! This is the whole source code of the compiler. This class can be used to translate one SX instruction (a 12-bit number) into a pointer to a function that will execute the instruction. The three ''execute'' member functions must be a hint to the sleight of hand that is taking place here: All the hard work of decoding the number into a real function call is performed by the same instruction decoder that was created before...
+
That's it! This is the whole source code of the compiler. This class can be used to translate one SX instruction (a 12-bit number) into a pointer to a function that will execute the instruction. The three ''execute'' member functions must be a hint to the sleight of hand that is taking place here: All the hard work of decoding the number into a real function call is performed by the same instruction decoder that was created before. This is because the class ''sx_compiler'' models the concept SXImplementation (it implements all the ''execute'' overloads), and as stated before, any class modeling that concept can be used as the target of an instruction decoder.
  
 
Below, you'll find an example that shows how the compiler is used together with an instruction decoder to compile one single SX instruction.
 
Below, you'll find an example that shows how the compiler is used together with an instruction decoder to compile one single SX instruction.
Line 349: Line 381:
 
</source>
 
</source>
  
{{Comments}}
+
==Comments==
 +
{{ShowComments|show=True}}

Latest revision as of 14:56, 23 February 2013

Sxgo constists of a core emulator library, a graphical UI and, alternatively, a set of Python bindings. This page describes the design of the core emulator. The implementation of this emulator revolves around a few main ideas:

  • a class that emulates the state of an SX, with member functions for each SX instruction.
  • a meta-programmed list of instructions
  • using meta programming to create a binary decision tree of those instructions for use in an instruction decoder.
  • precompilation of instructions into 'function pointers'.

I presume that the reader is familiar with the C++ programming language. Note that sxgo makes use of Template metaprogramming wherever suitable. Readers that are not familiar with MP may want to look through some of the examples in the wikipedia link...

Instruction Implementation

implementation of the SX emulator

The idea is to have one class that consists of an SX 'state' (ram, rom, special registers, program counter) and that implements the instruction set. See the snippet (of file sx_controller.hpp)below:

<source lang="cpp">

struct clr_fr { /*...*/ }; struct clr_w { /*...*/ }; struct mov_w_not_fr { /*...*/ }; // etc...

struct sx_controller_impl {

   // snip...
   // note that this is simplified source code, this does not set any 
   // flags yet.
   void execute( const clr_fr &, int arg_register)
   {
   	ram( arg_register) = 0;
   }
   void execute( const clr_w &)
   {
   	w = 0;
   }
   void execute( const mov_w_not_fr &, int arg_register)
   {
   	w = ~ram( arg_register);
   }
   // etc...

}; </source>

In C++ terms, this means that an implementation of the SX instruction set needs to model the Concept of SXImplementation. Where (informally) the concept of SXImplementation is any class that implements one of the following member functions for each of the tags that are in the SX instruction set. <source lang="cpp"> void execute( const tag &); void execute( const tag &, int arg); void execute( const tag &, int arg, int arg); </source> This concept is important, because as we shall see there are more models of this concept ('implementations of this interface') than just the sx emulator itself. We shall see that any class that models this concept can be used as the target of an instruction decoder, so that we can feed SX instruction words to a decoder and the decoder will call the correct overload of the execute function for such a class.

Alternative instruction implementations

Once we've decided the interface for SX instruction implementations, it becomes relatively easy to make different implementations. Sxgo defines several implementations, such as one implementation that does nothing more than return a string representation of the instructions (a disassembler), or one that 'compiles' instructions into a representation that will execute faster (a precompiler). All of these implementations can be fed through a single instruction decoder implementation.

Instruction set

Defining the SX instruction set boils down to two task: describing each SX instruction, specifically their bit patterns, and secondly compiling those instruction descriptions into a list of all SX instructions.

Instruction definitions

As could be seen in the previous Section, the implementation of the sx instruction set was all in terms of an execute method that is overloaded for several types, each type representing one instruction. Defining the instruction set becomes therefore a matter of defining a set of types, one for each instruction. These are the instruction tag types and the execute method is similar to a technique called tag dispatching.

In order to facilitate instruction decoding, the instruction types will be enhanced with information about their bit-patterns. This is in contrast with normal tag dispatching, where the tag types are completely empty types. The instruction list for sxgo can be found in sx_instruction_list.hpp and has the following form: <source lang="cpp"> // ... // define some operand (argument) types. struct register_ : masked_argument< 000011111> {}; struct bit_ : masked_argument< 011100000> {};

// ... // define some instruction word patterns struct clr_w : word< 000001000000> {}; struct add_w_fr : word< 0001110, register_> {}; struct sb_fr_bit : word< 0111 , register_, bit_> {};

// ... </source>

The code above encodes the following information:

operands (masked_argument<...>)
The code shows two operand types (there are more). In an SX microcontroller, operands are always encoded in the same position, registers are encoded in the least significant 5 bits of the instruction word and bit-positions (0-7) are encoded in bits 5, 6 and 7 of an instruction word.
word bit patterns (word<...>)
As can be seen above, the clr w instruction takes no arguments and is recognized by the bit-pattern "000001000000" (SX instructions are always 12 bit words). The add w, fr instruction takes one argument (the register, or ram location where the value can be found to add to the w register). Finally, the sb fr.bit instruction (set a single bit in a ram location) takes an 'fr' argument (the ram location) and a bit number. Note also that, although the bit number is more to the 'left' in the instruction word, the register argument is mentioned first for this instruction word.

Another way of describing this is, that the sb_fr_bit instruction codes an instruction word that looks like this: 0111bbbrrrrr, where