{"id":6089,"date":"2015-07-24T09:00:08","date_gmt":"2015-07-24T13:00:08","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6089"},"modified":"2017-08-11T13:23:54","modified_gmt":"2017-08-11T17:23:54","slug":"can-you-develop-your-own-processor-in-simulink","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2015\/07\/24\/can-you-develop-your-own-processor-in-simulink\/","title":{"rendered":"Can You Develop Your Own Processor in Simulink?"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/32620\">Greg<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/52199-mips-processor-in-simulink\">MIPS processor in Simulink<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2945398-mikhail\">Mikhail<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Processor simulation in Simulink<\/a><\/li>\r\n         <li><a href=\"#2\">What's so great about this model?<\/a><\/li>\r\n         <li><a href=\"#3\">It's really a tour of Turing<\/a><\/li>\r\n         <li><a href=\"#4\">Wait, I can use a program to write a program that runs a program that I have written in my own language?<\/a><\/li>\r\n         <li><a href=\"#5\">What in the name of Thor's Hammer is HDL?<\/a><\/li>\r\n         <li><a href=\"#6\">This seems confusing...<\/a><\/li>\r\n         <li><a href=\"#7\">What's your take on processor simulation?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Processor simulation in Simulink<a name=\"1\"><\/a><\/h3>\r\n   <p>Ever wonder how a microprocessor runs a program to perform computations?  Better yet, how would you go about building and\r\n      implementing such a design?\r\n   <\/p>\r\n   <p>Mikhail's example is simple and based on one published in a <a href=\"http:\/\/amzn.com\/0123944244\">book<\/a>. But it is a well laid out description of the computational process model using Simulink&reg; that supports both the simulation\r\n      of the program on your desktop machine, as well as the generation of HDL-code to deploy the design onto an FPGA.\r\n   <\/p>\r\n   <h3>What's so great about this model?<a name=\"2\"><\/a><\/h3>\r\n   <p>To me this is just super cool.  Perhaps that says more about me than this File Exchange entry.  I'm sure there's some marketing\r\n      message I should be spouting about how important simulation is to design. But really, what grabbed my attention is\r\n   <\/p>\r\n   <li>I can follow what was going on and watch the program execute in simulation<\/li>\r\n   <li>The model is neat and clean<\/li>\r\n   <li>I can see myself writing a little parser or compiler to program it<\/li>\r\n   <li>The resulting design can actually be implemented in hardware<\/li>\r\n   <p>Ultimately this means <i>I could create my own processor and programming language.<\/i><\/p>\r\n   <h3>It's really a tour of Turing<a name=\"3\"><\/a><\/h3>\r\n   <p>I very much like understanding how things work. I have thought a great deal about  processors, but perhaps hadn't considered\r\n      how they actually work in too much detail. After all, my background is mechanical engineering and not computer science.\r\n   <\/p>\r\n   <p>However... back in university, I took a philosophy course on artificial intelligence, and we spent a good deal of time developing\r\n      and discussing Turing machines.  (These have come back into the spotlight recently with the release of the movie about Alan\r\n      Turing).\r\n   <\/p>\r\n   <p>The basic premise behind a Turing machine is that an instruction can read a datum, perform some action with that datum, and\r\n      write a resulting datum into some location that can store the information.\r\n   <\/p>\r\n   <p>That is precisely what this model describes.  I suppose that's good, because a microprocessor is a Turing machine (well, a\r\n      finite version of a Turing machine)\r\n   <\/p>\r\n   <h3>Wait, I can use a program to write a program that runs a program that I have written in my own language?<a name=\"4\"><\/a><\/h3>\r\n   <p>It seems like there's some sort of circular dependency here, but the short answer is yes, you can do that. In fact Alan Turing\r\n      basically proved this notion with the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Universal_Turing_machine\">Universal Turing Machine<\/a>.\r\n   <\/p>\r\n   <p>You can develop an algorithm that represents fundamental operations. In this case the algorithm can be implemented on hardware\r\n      by generating HDL from the Simulink model.\r\n   <\/p>\r\n   <h3>What in the name of Thor's Hammer is HDL?<a name=\"5\"><\/a><\/h3>\r\n   <p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Hardware_description_language\">Hardware description language<\/a> (HDL) is a textual language to determine how a piece of electronics will operate by describing the structure of that device.\r\n   <\/p>\r\n   <p>In terms of silicon devices, it describes data pathways through which bits will flow and be stored.  <a href=\"https:\/\/en.wikipedia.org\/wiki\/Field-programmable_gate_array\">Field programmable gate arrays<\/a> (FPGAs) are unique silicon fabrications that can be \"reconfigured\" to a different set of data pathways (as opposed to your\r\n      computer's processor which has a fixed set of data pathways).\r\n   <\/p>\r\n   <p>In fact, processor designers and developers use HDL to create and implement processor designs.  Often HDL appears as a very\r\n      low-level language because you are often dealing at the level of individual bits.\r\n   <\/p>\r\n   <h3>This seems confusing...<a name=\"6\"><\/a><\/h3>\r\n   <p>It's clear that these concepts are quite abstract, which is why I found the MIPS model so interesting. I could begin to deconstruct\r\n      how the processor will ultimately parse and respond to an instruction.\r\n   <\/p>\r\n   <p>I used the fact this algorithm is in Simulink to interrogate different aspects of the processor language design such as,<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>What is the current processor instruction at any given time in the program?<\/li>\r\n   <\/div>\r\n\r\n   <p><img decoding=\"async\" width=\"600\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/programStatus.png\"> <\/p>\r\n   <div>\r\n         <li>What is the value of the program counter?<\/li>\r\n         <li>Where in memory is the current data being read?<\/li>\r\n         <li>Is data being read or written?<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>I even fantasized about writing my own compiler for this using MATLAB <a href=\"\">regular expressions<\/a> or perhaps <a href=\"http:\/\/www.antlr.org\/\">ANTLR<\/a>, but writing these blog posts is hard enough!\r\n   <\/p>\r\n   <h3>What's your take on processor simulation?<a name=\"7\"><\/a><\/h3>\r\n   <p>Have you ever built your own processor? Would you be interested in simulating how a processor works? Do you believe this approach\r\n      in Simulink is scalable?\r\n   <\/p>\r\n   <p>Leave your comments <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=6089#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_6899fb360b1b403fa65f5988abde56a4() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='6899fb360b1b403fa65f5988abde56a4 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 6899fb360b1b403fa65f5988abde56a4';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Greg Wolff';\r\n        copyright = 'Copyright 2015 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_6899fb360b1b403fa65f5988abde56a4()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 8.5<br><\/p>\r\n<\/div>\r\n<!--\r\n6899fb360b1b403fa65f5988abde56a4 ##### SOURCE BEGIN #####\r\n%% Can You Develop Your Own Processor in Simulink?\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/32620 Greg's> pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/52199-mips-processor-in-simulink\r\n% MIPS processor in Simulink>\r\n% by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2945398-mikhail Mikhail>.\r\n% \r\n%% Processor simulation in Simulink\r\n% Ever wonder how a microprocessor runs a program to perform computations?  Better yet, how would you go about building and implementing such a design?\r\n% \r\n% Mikhail's example is simple and based on one published in a <http:\/\/amzn.com\/0123944244 book>. But it is a well laid out description of the computational process model using Simulink&reg; that supports both the simulation of the program on your desktop machine, as well as the generation of HDL-code to deploy the design onto an FPGA.\r\n% \r\n%% What's so great about this model?\r\n% To me this is just super cool.  Perhaps that says more about me than this File Exchange entry.  I'm sure there's some marketing message I should be spouting about how important simulation is to design. But really, what grabbed my attention is\r\n% \r\n% # I can follow what was going on and watch the program execute in simulation\r\n% # The model is neat and clean\r\n% # I can see myself writing a little parser or compiler to program it\r\n% # The resulting design can actually be implemented in hardware\r\n% \r\n% Ultimately this means _I could create my own processor and programming language._\r\n% \r\n%% It's really a tour of Turing\r\n% I very much like understanding how things work. I have thought a great deal about  processors, but perhaps hadn't considered how they actually work in too much detail. After all, my background is mechanical engineering and not computer science.\r\n% \r\n% However... back in university, I took a philosophy course on artificial intelligence, and we spent a good deal of time developing and discussing Turing machines.  (These have come back into the spotlight recently with the release of the movie about Alan Turing).\r\n% \r\n% The basic premise behind a Turing machine is that an instruction can read a datum, perform some action with that datum, and write a resulting datum into some location that can store the information.\r\n% \r\n% That is precisely what this model describes.  I suppose that's good, because a microprocessor is a Turing machine (well, a finite version of a Turing machine)\r\n% \r\n%% Wait, I can use a program to write a program that runs a program that I have written in my own language?\r\n% It seems like there's some sort of circular dependency here, but the short answer is yes, you can do that. In fact Alan Turing basically proved this notion with the <https:\/\/en.wikipedia.org\/wiki\/Universal_Turing_machine Universal Turing Machine>.\r\n% \r\n% You can develop an algorithm that represents fundamental operations. In this case the algorithm can be implemented on hardware by generating HDL from the Simulink model.\r\n% \r\n%% What in the name of Thor's Hammer is HDL?\r\n% <https:\/\/en.wikipedia.org\/wiki\/Hardware_description_language Hardware description language> (HDL) is a textual language to determine how a piece of electronics will operate by describing the structure of that device.\r\n% \r\n% In terms of silicon devices, it describes data pathways through which bits will flow and be stored.  <https:\/\/en.wikipedia.org\/wiki\/Field-programmable_gate_array Field programmable gate arrays> (FPGAs) are unique silicon fabrications that can be \"reconfigured\" to a different set of data pathways (as opposed to your computer's processor which has a fixed set of data pathways).\r\n% \r\n% In fact, processor designers and developers use HDL to create and implement processor designs.  Often HDL appears as a very low-level language because you are often dealing at the level of individual bits.\r\n% \r\n%% This seems confusing...\r\n% It's clear that these concepts are quite abstract, which is why I found the MIPS model so interesting. I could begin to deconstruct how the processor will ultimately parse and respond to an instruction.\r\n% \r\n% I used the fact this algorithm is in Simulink to interrogate different aspects of the processor language design such as,\r\n% \r\n% * What is the current processor instruction at any given time in the program?\r\n% * What is the value of the program counter?\r\n% * Where in memory is the current data being read?\r\n% * Is data being read or written?\r\n% \r\n% I even fantasized about writing my own compiler for this using MATLAB < regular expressions> or perhaps <http:\/\/www.antlr.org\/ ANTLR>, but writing these blog posts is hard enough!\r\n% \r\n%% What's your take on processor simulation?\r\n% Have you ever built your own processor? Would you be interested in simulating how a processor works? Do you believe this approach in Simulink is scalable?\r\n% \r\n% Leave your comments <https:\/\/blogs.mathworks.com\/pick\/ here>.\r\n\r\n##### SOURCE END ##### 6899fb360b1b403fa65f5988abde56a4\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/programStatus.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Greg's pick this week is MIPS processor in Simulink by Mikhail.\r\n      \r\n   \r\n   Contents\r\n   \r\n      \r\n        ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/07\/24\/can-you-develop-your-own-processor-in-simulink\/\">read more >><\/a><\/p>","protected":false},"author":36,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6089"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=6089"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6089\/revisions"}],"predecessor-version":[{"id":8809,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6089\/revisions\/8809"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}