{"id":458,"date":"2013-04-28T12:37:57","date_gmt":"2013-04-28T12:37:57","guid":{"rendered":"http:\/\/retroramblings.net\/?p=458"},"modified":"2013-05-29T19:41:07","modified_gmt":"2013-05-29T19:41:07","slug":"more-zpu-experiments","status":"publish","type":"post","link":"http:\/\/retroramblings.net\/?p=458","title":{"rendered":"More ZPU experiments"},"content":{"rendered":"<p>Since my last post I&#8217;ve been continuing to work on the ZPU small core, adding hardware implementations of the optional instructions while trying to keep the core as small as possible.<\/p>\n<p>Certain instructions are closely related enough that once one is implemented the others come almost for free. Sub, Eq, Neq, Lessthan and Lessthanorequal come under that category.<br \/>\n<!--more--><br \/>\nThe simplest way to compare two values is to subtract one from the other, and see whether the result is positive, negative or zero, so all five instructions need the result of a subtraction: (sp+1)-(sp).<\/p>\n<p>Eq and Neq could be implemented as a direct comparison, but if the result of the subtraction is available anyway, we can compare that result against zero, which is theoretically more efficient.  (There&#8217;s a good chance that even if we did a bitwise comparison, the synthesis tool would spot that the subtraction result&#8217;s available and use it anyway &#8211; the tools are very smart these days!)<\/p>\n<p>Thus we perform the subtraction on every rising clock edge, like so:<\/p>\n<pre>if COMPARISON_SUB=true then\r\n\tcomparison_sub_result<=unsigned(memBRead(wordSize-1)&#038;memBRead)\r\n\t\t- unsigned(memARead(wordSize-1)&#038;memARead);\r\nend if;<\/pre>\n<p>We make comparison_sub_result 33 bits wide rather than 32 because we need an extra bit for signed comparisons. If we were only doing unsigned comparisons we wouldn't need to worry about this.<\/p>\n<p>Since we also need to know whether this result is zero to implement four of the instructions, we'll make a comparison_eq signal and assign it like so. This is done outside a clock edge, so it's combinatorial:<\/p>\n<pre>if COMPARISON_SUB=true and comparison_sub_result='0'&X\"00000000\" then\r\n\tcomparison_eq<='1';\r\nelse\r\n\tcomparison_eq<='0';\r\nend if;<\/pre>\n<p>The implementation of the instructions is surprisingly straightforward. Sub is the simplest:<\/p>\n<pre>when State_Sub =>\r\n\tmemAAddr        <= sp;\r\n\tmemAWriteEnable <= '1';\r\n\tmemAWrite       <= comparison_sub_result(wordSize-1 downto 0);\r\n\tstate           <= State_Fetch;\r\n<\/pre>\n<p>Eq and Neq are also fairly straightforward. The two instructions are complementary, Eq pushes 1 onto the stack if the two operands are equal, and 0 otherwise. Neq reverses this, pushing 0 if the operands are equal. We could implement the two instructions separately, but it's also possible to implement them together, like so:<\/p>\n<pre>when State_EqNeq =>\r\n\tmemAAddr <= sp;\r\n\tmemAWriteEnable <= '1';\r\n\tmemAWrite       <= (others =>'0');\r\n\tmemAWrite(0) <= comparison_eq xor opcode(4);\r\n\tstate <= State_Fetch;<\/pre>\n<p>The opcode for Eq is 46 (\"101110\"), and the opcode for Neq is 48 (\"110000\"), so to reverse the sense of the text for \"Neq\" we can use a simple exclusive-or against opcode bit 4.<\/p>\n<p>Lessthan and Lessthanorequal are a bit trickier, because they perform signed comparison.<br \/>\nUnsigned comparison is simple - we just have to subtract one operand from the other, and check the highest bit of the result, which tells us whether the result underflowed. For signed comparison we can do exactly the same, but we need one extra bit's headroom in the subtraction result.\u00a0 That highest bit is zero if op1 is less than or equal to op2, so we need to invert it.\u00a0 We also need to take care of the difference between lessthan and lessthanorequal.\u00a0 The code for signed comparison ends up looking like this:<\/p>\n<pre>when State_Comparison =&gt;\r\n\tmemAAddr &lt;= sp;\r\n\tmemAWriteEnable &lt;= '1';\r\n\tmemAWrite &lt;= (others =&gt; '0');\r\n\tmemAWrite(0) &lt;= not (comparison_sub_result(wordSize)\r\n\t\txor (not opCode(0) and comparison_eq));\r\n\tstate &lt;= State_Fetch;<\/pre>\n<p>So how does the core perform with these changes?<br \/>\nMy current test case which writes to the framebuffer in halfwords performs like this:<\/p>\n<ul>\n<li>Emulated sub, eq, lessthan, etc., and emulated eqbranch\/neqbranch: 0.58 fps (579 logic elements)<\/li>\n<li>Hardware sub, eq, lessthen, etc., and hardware eqbranch\/newbranch: 1.55 fps (745 logic elements)<\/li>\n<\/ul>\n<p>Full source for the project <a href=\"https:\/\/github.com\/robinsonb5\/ZPUTest\">can be found on github<\/a> for anyone that's interested.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since my last post I&#8217;ve been continuing to work on the ZPU small core, adding hardware implementations of the optional instructions while trying to keep the core as small as possible. Certain instructions are closely related enough that once one &hellip; <a href=\"http:\/\/retroramblings.net\/?p=458\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-458","post","type-post","status-publish","format-standard","hentry","category-fpga"],"_links":{"self":[{"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts\/458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=458"}],"version-history":[{"count":19,"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts\/458\/revisions"}],"predecessor-version":[{"id":545,"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts\/458\/revisions\/545"}],"wp:attachment":[{"href":"http:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=458"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}