/* ewok.cc -- * * Ewok is a 'wide insruction word' architecture. * Each 32-bit instruction contains 8 4-bit instructions; * see "inc, dec, clr, neg, get, add, mul, ble" below. * Programs are 1024 bytes; that is, 256 32-bit instructions. * # Copyright (c) 2006 Henry Strickland -- in the domain # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. # # (* http://www.opensource.org/licenses/mit-license.php *) */ #include "li.h" namespace Ewok { using namespace Li; #define RMAX 16 #define RMASK (RMAX-1) #define PMAX 256 #define PCWIDTH 8 typedef unsigned char byte; typedef unsigned int un; typedef signed int si; class EwokMachine : public Machine { vector code; si reg[RMAX]; protected: virtual ~EwokMachine(); virtual void eval_virtual( ); public: EwokMachine(Link p, Link c ); void setCodeFromString( string program ); void ew_loop(); }; EwokMachine::~EwokMachine() {} EwokMachine::EwokMachine(Link p, Link c ) : Machine(p,c) { memset( reg, 0, sizeof reg ); setCodeFromString( m_creature->sourceCode ); } // for backwards compatibilty, where program is 256 bytes void EwokMachine::setCodeFromString( string program ) { // Incomplete trailing digits will not be used, // nor will digits beyond NUM_CODE const char* s= program.c_str(); word slen= program.size(); un i= 0; do { un tmp= 0; for (un nyb=0; nyb=slen ? 0 : hex_char_to_word(s[i]) ); ++i; } code.push_back(tmp); } while ( i>28) & RMASK; //printf("Inc = %u ", Inc ); un Dec = (code[pc]>>24) & RMASK; //printf("Dec = %u ", Dec ); un clr = (code[pc]>>20) & RMASK; un neg = (code[pc]>>16) & RMASK; un get = (code[pc]>>12) & RMASK; un add = (code[pc]>> 8) & RMASK; un mul = (code[pc]>> 4) & RMASK; un ble = (code[pc]>> 0) & RMASK; assert(Inc>=0); assert(Dec>=0); //printf("I %u ", Inc ); assert(Inc defaultParams( ); virtual Link createMachine( Link p, Link c ); } EwokLanguage::Singleton; Link EwokLanguage::createMachine( Link p, Link c ) { return new EwokMachine( p, c ); } Link EwokLanguage::defaultParams( ) { Link p= Language::defaultParams(); p->creatureAlphabet = "01234567"; return p; } void EwokMachine::eval_virtual( ) { ew_loop(); } TEST(ewok1) { Language* lang= Language::Find("ewok"); Link pp= lang->defaultParams(); Creature* prog= new Creature("123456789a", lang); Link mach= lang->createMachine( pp, prog ); mach->eval(); #if 0 Link expect = new Numbers(); expect->vec.push_back(3); AssertStrEq( ToString(expect->vec), ToString(mach->m_outputs->vec) ); AssertEq( 1u, mach->m_outputs->vec.size() ); AssertEq( 3, mach->m_outputs->vec.at(0) ); #endif } }//Li