Download | Plain Text | No Line Numbers


  1. /**
  2.  * @module instructions
  3.  * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
  4.  * @brief Implementations of CInstruction
  5.  * @date 26.05.2009
  6.  */
  7.  
  8. #ifndef INSTRUCTIONS_H
  9. #define INSTRUCTIONS_H 1
  10.  
  11. #include "cinstruction.h"
  12. #include "ccpu.h"
  13. #include "cprogram.h"
  14.  
  15. /**
  16.  * @class CInstructionInc
  17.  *
  18.  * Implementation of assembler command "inc"
  19.  * Syntax: inc R1
  20.  * (R1++)
  21.  */
  22. template <class T>
  23. class CInstructionInc
  24. : public CInstruction<T>
  25. {
  26. typedef CInstruction<T> super;
  27.  
  28. public:
  29. CInstructionInc()
  30. : CInstruction<T>("inc")
  31. {}
  32.  
  33. CInstructionInc *factory()
  34. {
  35. return new CInstructionInc;
  36. }
  37.  
  38. void compile(std::list<std::string>& params);
  39. void execute(CCPU<T> *cpu);
  40.  
  41. protected:
  42. /** register number */
  43. unsigned m_regidx1;
  44. };
  45.  
  46. /*----------------------------------------------------------------------------*/
  47.  
  48. template <class T>
  49. void CInstructionInc<T>::compile(std::list<std::string>& params)
  50. {
  51. if (params.size() != 1)
  52. throw CInstructionError("Invalid paramater count - must be 1");
  53. m_regidx1 = super::parseRegister(params.front());
  54. params.pop_front();
  55. }
  56.  
  57. /*----------------------------------------------------------------------------*/
  58.  
  59. template <class T>
  60. void CInstructionInc<T>::execute(CCPU<T> *cpu)
  61. {
  62. assert(cpu != NULL);
  63. super::checkRegister(cpu, m_regidx1);
  64. cpu->getRegisters()[ m_regidx1 ]++;
  65. }
  66.  
  67. /*============================================================================*/
  68.  
  69. /**
  70.  * @class CInstructionDec
  71.  *
  72.  * Implementation of assembler command "dec"
  73.  * Syntax: dec R1
  74.  * (R1--)
  75.  */
  76. template <class T>
  77. class CInstructionDec
  78. : public CInstruction<T>
  79. {
  80. typedef CInstruction<T> super;
  81.  
  82. public:
  83. CInstructionDec()
  84. : CInstruction<T>("dec")
  85. {}
  86.  
  87. CInstructionDec *factory()
  88. {
  89. return new CInstructionDec;
  90. }
  91.  
  92. void compile(std::list<std::string>& params);
  93. void execute(CCPU<T> *cpu);
  94.  
  95. protected:
  96. /** register number */
  97. unsigned m_regidx1;
  98. };
  99.  
  100. /*----------------------------------------------------------------------------*/
  101.  
  102. template <class T>
  103. void CInstructionDec<T>::compile(std::list<std::string>& params)
  104. {
  105. if (params.size() != 1)
  106. throw CInstructionError("Invalid paramater count - must be 1");
  107. m_regidx1 = super::parseRegister(params.front());
  108. params.pop_front();
  109. }
  110.  
  111. /*----------------------------------------------------------------------------*/
  112.  
  113. template <class T>
  114. void CInstructionDec<T>::execute(CCPU<T> *cpu)
  115. {
  116. assert(cpu != NULL);
  117. super::checkRegister(cpu, m_regidx1);
  118. cpu->getRegisters()[ m_regidx1 ]--;
  119. }
  120.  
  121. /*============================================================================*/
  122.  
  123. /**
  124.  * @class CInstructionAdd
  125.  *
  126.  * Implementation of assembler command "add"
  127.  * Syntax: add R1, R2, R3
  128.  * (R1 = R2 + R3)
  129.  */
  130. template <class T>
  131. class CInstructionAdd
  132. : public CInstruction<T>
  133. {
  134. typedef CInstruction<T> super;
  135.  
  136. public:
  137. CInstructionAdd()
  138. : CInstruction<T>("add")
  139. {}
  140.  
  141. CInstructionAdd *factory()
  142. {
  143. return new CInstructionAdd;
  144. }
  145.  
  146. void compile(std::list<std::string>& params);
  147. void execute(CCPU<T> *cpu);
  148.  
  149. protected:
  150. /** register number */
  151. unsigned m_regidx1;
  152. /** register number */
  153. unsigned m_regidx2;
  154. /** register number */
  155. unsigned m_regidx3;
  156. };
  157.  
  158. /*----------------------------------------------------------------------------*/
  159.  
  160. template <class T>
  161. void CInstructionAdd<T>::compile(std::list<std::string>& params)
  162. {
  163. if (params.size() != 3)
  164. throw CInstructionError("Invalid paramater count - must be 3");
  165. m_regidx1 = super::parseRegister(params.front());
  166. params.pop_front();
  167. m_regidx2 = super::parseRegister(params.front());
  168. params.pop_front();
  169. m_regidx3 = super::parseRegister(params.front());
  170. params.pop_front();
  171. }
  172.  
  173. /*----------------------------------------------------------------------------*/
  174.  
  175. template <class T>
  176. void CInstructionAdd<T>::execute(CCPU<T> *cpu)
  177. {
  178. assert(cpu != NULL);
  179. super::checkRegister(cpu, m_regidx1);
  180. super::checkRegister(cpu, m_regidx2);
  181. super::checkRegister(cpu, m_regidx3);
  182. cpu->getRegisters()[ m_regidx1 ] = cpu->getRegisters()[ m_regidx2 ]
  183. + cpu->getRegisters()[ m_regidx3 ];
  184. }
  185.  
  186. /*============================================================================*/
  187.  
  188. /**
  189.  * @class CInstructionSub
  190.  *
  191.  * Implementation of assembler command "sub"
  192.  * Syntax: sub R1, R2, R3
  193.  * (R1 = R2 - R3)
  194.  */
  195. template <class T>
  196. class CInstructionSub
  197. : public CInstruction<T>
  198. {
  199. typedef CInstruction<T> super;
  200.  
  201. public:
  202. CInstructionSub()
  203. : CInstruction<T>("sub")
  204. {}
  205.  
  206. CInstructionSub *factory()
  207. {
  208. return new CInstructionSub;
  209. }
  210.  
  211. void compile(std::list<std::string>& params);
  212. void execute(CCPU<T> *cpu);
  213.  
  214. protected:
  215. /** register number */
  216. unsigned m_regidx1;
  217. /** register number */
  218. unsigned m_regidx2;
  219. /** register number */
  220. unsigned m_regidx3;
  221. };
  222.  
  223. /*----------------------------------------------------------------------------*/
  224.  
  225. template <class T>
  226. void CInstructionSub<T>::compile(std::list<std::string>& params)
  227. {
  228. if (params.size() != 3)
  229. throw CInstructionError("Invalid paramater count - must be 3");
  230. m_regidx1 = super::parseRegister(params.front());
  231. params.pop_front();
  232. m_regidx2 = super::parseRegister(params.front());
  233. params.pop_front();
  234. m_regidx3 = super::parseRegister(params.front());
  235. params.pop_front();
  236. }
  237.  
  238. /*----------------------------------------------------------------------------*/
  239.  
  240. template <class T>
  241. void CInstructionSub<T>::execute(CCPU<T> *cpu)
  242. {
  243. assert(cpu != NULL);
  244. super::checkRegister(cpu, m_regidx1);
  245. super::checkRegister(cpu, m_regidx2);
  246. super::checkRegister(cpu, m_regidx3);
  247. cpu->getRegisters()[ m_regidx1 ] = cpu->getRegisters()[ m_regidx2 ]
  248. - cpu->getRegisters()[ m_regidx3 ];
  249. }
  250.  
  251. /*============================================================================*/
  252.  
  253. /**
  254.  * @class CInstructionMul
  255.  *
  256.  * Implementation of assembler command "mul"
  257.  * Syntax: mul R1, R2, R3
  258.  * (R1 = R2 * R3)
  259.  */
  260. template <class T>
  261. class CInstructionMul
  262. : public CInstruction<T>
  263. {
  264. typedef CInstruction<T> super;
  265.  
  266. public:
  267. CInstructionMul()
  268. : CInstruction<T>("mul")
  269. {}
  270.  
  271. CInstructionMul *factory()
  272. {
  273. return new CInstructionMul;
  274. }
  275.  
  276. void compile(std::list<std::string>& params);
  277. void execute(CCPU<T> *cpu);
  278.  
  279. protected:
  280. /** register number */
  281. unsigned m_regidx1;
  282. /** register number */
  283. unsigned m_regidx2;
  284. /** register number */
  285. unsigned m_regidx3;
  286. };
  287.  
  288. /*----------------------------------------------------------------------------*/
  289.  
  290. template <class T>
  291. void CInstructionMul<T>::compile(std::list<std::string>& params)
  292. {
  293. if (params.size() != 3)
  294. throw CInstructionError("Invalid paramater count - must be 3");
  295. m_regidx1 = super::parseRegister(params.front());
  296. params.pop_front();
  297. m_regidx2 = super::parseRegister(params.front());
  298. params.pop_front();
  299. m_regidx3 = super::parseRegister(params.front());
  300. params.pop_front();
  301. }
  302.  
  303. /*----------------------------------------------------------------------------*/
  304.  
  305. template <class T>
  306. void CInstructionMul<T>::execute(CCPU<T> *cpu)
  307. {
  308. super::checkRegister(cpu, m_regidx1);
  309. super::checkRegister(cpu, m_regidx2);
  310. super::checkRegister(cpu, m_regidx3);
  311. cpu->getRegisters()[ m_regidx1 ] = cpu->getRegisters()[ m_regidx2 ]
  312. * cpu->getRegisters()[ m_regidx3 ];
  313. }
  314.  
  315. /*============================================================================*/
  316.  
  317. /**
  318.  * @class CInstructionDiv
  319.  *
  320.  * Implementation of assembler command "div"
  321.  * Syntax: div R1, R2, R3
  322.  * (R1 = R2 / R3)
  323.  */
  324. template <class T>
  325. class CInstructionDiv
  326. : public CInstruction<T>
  327. {
  328. typedef CInstruction<T> super;
  329.  
  330. public:
  331. CInstructionDiv()
  332. : CInstruction<T>("div")
  333. {}
  334.  
  335. CInstructionDiv *factory()
  336. {
  337. return new CInstructionDiv;
  338. }
  339.  
  340. void compile(std::list<std::string>& params);
  341. void execute(CCPU<T> *cpu);
  342.  
  343. protected:
  344. /** register number */
  345. unsigned m_regidx1;
  346. /** register number */
  347. unsigned m_regidx2;
  348. /** register number */
  349. unsigned m_regidx3;
  350. };
  351.  
  352. /*----------------------------------------------------------------------------*/
  353.  
  354. template <class T>
  355. void CInstructionDiv<T>::compile(std::list<std::string>& params)
  356. {
  357. if (params.size() != 3)
  358. throw CInstructionError("Invalid paramater count - must be 3");
  359. m_regidx1 = super::parseRegister(params.front());
  360. params.pop_front();
  361. m_regidx2 = super::parseRegister(params.front());
  362. params.pop_front();
  363. m_regidx3 = super::parseRegister(params.front());
  364. params.pop_front();
  365. }
  366.  
  367. /*----------------------------------------------------------------------------*/
  368.  
  369. template <class T>
  370. void CInstructionDiv<T>::execute(CCPU<T> *cpu)
  371. {
  372. assert(cpu != NULL);
  373. super::checkRegister(cpu, m_regidx1);
  374. super::checkRegister(cpu, m_regidx2);
  375. super::checkRegister(cpu, m_regidx3);
  376. cpu->getRegisters()[ m_regidx1 ] = cpu->getRegisters()[ m_regidx2 ]
  377. / cpu->getRegisters()[ m_regidx3 ];
  378. }
  379.  
  380. /*============================================================================*/
  381.  
  382. /**
  383.  * @class CInstructionLoad
  384.  *
  385.  * Implementation of assembler command "load"
  386.  * Syntax: load R1, R2
  387.  * (R1 = memory[R2])
  388.  */
  389. template <class T>
  390. class CInstructionLoad
  391. : public CInstruction<T>
  392. {
  393. typedef CInstruction<T> super;
  394.  
  395. public:
  396. CInstructionLoad()
  397. : CInstruction<T>("load")
  398. {}
  399.  
  400. CInstructionLoad *factory()
  401. {
  402. return new CInstructionLoad;
  403. }
  404.  
  405. void compile(std::list<std::string>& params);
  406. void execute(CCPU<T> *cpu);
  407.  
  408. protected:
  409. /** register number */
  410. unsigned m_regidx1;
  411. /** register number */
  412. unsigned m_regidx2;
  413. };
  414.  
  415. /*----------------------------------------------------------------------------*/
  416.  
  417. template <class T>
  418. void CInstructionLoad<T>::compile(std::list<std::string>& params)
  419. {
  420. if (params.size() != 2)
  421. throw CInstructionError("Invalid paramater count - must be 2");
  422. m_regidx1 = super::parseRegister(params.front());
  423. params.pop_front();
  424. m_regidx2 = super::parseRegister(params.front());
  425. params.pop_front();
  426. }
  427.  
  428. /*----------------------------------------------------------------------------*/
  429.  
  430. template <class T>
  431. void CInstructionLoad<T>::execute(CCPU<T> *cpu)
  432. {
  433. assert(cpu != NULL);
  434. assert(cpu->getMemory() != NULL);
  435. super::checkRegister(cpu, m_regidx1);
  436. super::checkRegister(cpu, m_regidx2);
  437. T val(cpu->getRegisters()[ m_regidx2 ]);
  438. cpu->getRegisters()[ m_regidx1 ] = (*cpu->getMemory())[ val ];
  439. }
  440.  
  441. /*============================================================================*/
  442.  
  443. /**
  444.  * @class CInstructionStore
  445.  *
  446.  * Implementation of assembler command "store"
  447.  * Syntax: store R1, R2
  448.  * (memory[R2] = R1)
  449.  */
  450. template <class T>
  451. class CInstructionStore
  452. : public CInstruction<T>
  453. {
  454. typedef CInstruction<T> super;
  455.  
  456. public:
  457. CInstructionStore()
  458. : CInstruction<T>("store")
  459. {}
  460.  
  461. CInstructionStore *factory()
  462. {
  463. return new CInstructionStore;
  464. }
  465.  
  466. void compile(std::list<std::string>& params);
  467. void execute(CCPU<T> *cpu);
  468.  
  469. protected:
  470. /** register number */
  471. unsigned m_regidx1;
  472. /** register number */
  473. unsigned m_regidx2;
  474. };
  475.  
  476. /*----------------------------------------------------------------------------*/
  477.  
  478. template <class T>
  479. void CInstructionStore<T>::compile(std::list<std::string>& params)
  480. {
  481. if (params.size() != 2)
  482. throw CInstructionError("Invalid paramater count - must be 2");
  483. m_regidx1 = super::parseRegister(params.front());
  484. params.pop_front();
  485. m_regidx2 = super::parseRegister(params.front());
  486. params.pop_front();
  487. }
  488.  
  489. /*----------------------------------------------------------------------------*/
  490.  
  491. template <class T>
  492. void CInstructionStore<T>::execute(CCPU<T> *cpu)
  493. {
  494. assert(cpu != NULL);
  495. assert(cpu->getMemory() != NULL);
  496. super::checkRegister(cpu, m_regidx1);
  497. super::checkRegister(cpu, m_regidx2);
  498. T val(cpu->getRegisters()[ m_regidx2 ]);
  499. (*cpu->getMemory())[ val ] = cpu->getRegisters()[ m_regidx1 ];
  500. }
  501.  
  502. /*============================================================================*/
  503.  
  504. /**
  505.  * @class CInstructionTest
  506.  *
  507.  * Implementation of assembler command "test"
  508.  * Syntax: test R1
  509.  * (R1 == 0: zeroflag: true, R1 < 0: signflag: true)
  510.  */
  511. template <class T>
  512. class CInstructionTest
  513. : public CInstruction<T>
  514. {
  515. typedef CInstruction<T> super;
  516.  
  517. public:
  518. CInstructionTest()
  519. : CInstruction<T>("test")
  520. {}
  521.  
  522. CInstructionTest *factory()
  523. {
  524. return new CInstructionTest;
  525. }
  526.  
  527. void compile(std::list<std::string>& params);
  528. void execute(CCPU<T> *cpu);
  529.  
  530. protected:
  531. /** register number */
  532. unsigned m_regidx1;
  533. };
  534.  
  535. /*----------------------------------------------------------------------------*/
  536.  
  537. template <class T>
  538. void CInstructionTest<T>::compile(std::list<std::string>& params)
  539. {
  540. if (params.size() != 1)
  541. throw CInstructionError("Invalid paramater count - must be 1");
  542. m_regidx1 = super::parseRegister(params.front());
  543. params.pop_front();
  544. }
  545.  
  546. /*----------------------------------------------------------------------------*/
  547.  
  548. template <class T>
  549. void CInstructionTest<T>::execute(CCPU<T> *cpu)
  550. {
  551. assert(cpu != NULL);
  552. super::checkRegister(cpu, m_regidx1);
  553. if (cpu->getRegisters()[ m_regidx1 ] == T(0))
  554. cpu->setFlagZero(true);
  555. if (cpu->getRegisters()[ m_regidx1 ] < T(0))
  556. cpu->setFlagSign(true);
  557. }
  558.  
  559. /*============================================================================*/
  560.  
  561. /**
  562.  * @class CInstructionLabel
  563.  *
  564.  * Implementation of assembler command "label"
  565.  * Syntax: label name:
  566.  */
  567. template <class T>
  568. class CInstructionLabel
  569. : public CInstruction<T>
  570. {
  571. typedef CInstruction<T> super;
  572.  
  573. public:
  574. CInstructionLabel()
  575. : CInstruction<T>("label")
  576. {}
  577.  
  578. CInstructionLabel *factory()
  579. {
  580. return new CInstructionLabel;
  581. }
  582.  
  583. void compile(std::list<std::string>& params)
  584. {}
  585.  
  586. void execute(CCPU<T> *cpu)
  587. {}
  588. };
  589.  
  590. /*============================================================================*/
  591.  
  592. /**
  593.  * @class CInstructionJumpA
  594.  *
  595.  * Implementation of assembler command "jumpa"
  596.  * Syntax: jumpa labelname
  597.  * (jump to labelname)
  598.  */
  599. template <class T>
  600. class CInstructionJumpA
  601. : public CInstruction<T>
  602. {
  603. typedef CInstruction<T> super;
  604.  
  605. public:
  606. CInstructionJumpA()
  607. : CInstruction<T>("jumpa"), m_addr("")
  608. {}
  609.  
  610. CInstructionJumpA *factory()
  611. {
  612. return new CInstructionJumpA;
  613. }
  614.  
  615. void compile(std::list<std::string>& params);
  616. void execute(CCPU<T> *cpu);
  617.  
  618. protected:
  619. /** labelname */
  620. std::string m_addr;
  621. };
  622.  
  623. /*----------------------------------------------------------------------------*/
  624.  
  625. template <class T>
  626. void CInstructionJumpA<T>::compile(std::list<std::string>& params)
  627. {
  628. if (params.size() != 1)
  629. throw CInstructionError("Invalid paramater count - must be 1");
  630. m_addr = params.front();
  631. params.pop_front();
  632. }
  633.  
  634. /*----------------------------------------------------------------------------*/
  635.  
  636. template <class T>
  637. void CInstructionJumpA<T>::execute(CCPU<T> *cpu)
  638. {
  639. assert(cpu != NULL);
  640. assert(cpu->getProgram() != NULL);
  641. if (m_addr.empty())
  642. throw CInstructionError("Empty address");
  643. try
  644. {
  645. cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr);
  646. }
  647. catch(CProgramError& ex)
  648. {
  649. throw CInstructionError(ex.what());
  650. }
  651. }
  652.  
  653. /*============================================================================*/
  654.  
  655. /**
  656.  * @class CInstructionJumpZ
  657.  *
  658.  * Implementation of assembler command "jumpz"
  659.  * Syntax: jumpz labelname
  660.  * (jump to labelname if zeroflag)
  661.  */
  662. template <class T>
  663. class CInstructionJumpZ
  664. : public CInstruction<T>
  665. {
  666. typedef CInstruction<T> super;
  667.  
  668. public:
  669. CInstructionJumpZ()
  670. : CInstruction<T>("jumpz"), m_addr("")
  671. {}
  672.  
  673. CInstructionJumpZ *factory()
  674. {
  675. return new CInstructionJumpZ;
  676. }
  677.  
  678. void compile(std::list<std::string>& params);
  679. void execute(CCPU<T> *cpu);
  680.  
  681. protected:
  682. /** labelname */
  683. std::string m_addr;
  684. };
  685.  
  686. /*----------------------------------------------------------------------------*/
  687.  
  688. template <class T>
  689. void CInstructionJumpZ<T>::compile(std::list<std::string>& params)
  690. {
  691. if (params.size() != 1)
  692. throw CInstructionError("Invalid paramater count - must be 1");
  693. m_addr = params.front();
  694. params.pop_front();
  695. }
  696.  
  697. /*----------------------------------------------------------------------------*/
  698.  
  699. template <class T>
  700. void CInstructionJumpZ<T>::execute(CCPU<T> *cpu)
  701. {
  702. assert(cpu != NULL);
  703. assert(cpu->getProgram() != NULL);
  704. if (!cpu->getFlagZero())
  705. return;
  706. if (m_addr.empty())
  707. throw CInstructionError("Empty address");
  708. try
  709. {
  710. cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr);
  711. }
  712. catch(CProgramError& ex)
  713. {
  714. throw CInstructionError(ex.what());
  715. }
  716. }
  717.  
  718. /*============================================================================*/
  719.  
  720. /**
  721.  * @class CInstructionJumpS
  722.  *
  723.  * Implementation of assembler command "jumps"
  724.  * Syntax: jumps labelname
  725.  * (jump to labelname if signflag)
  726.  */
  727. template <class T>
  728. class CInstructionJumpS
  729. : public CInstruction<T>
  730. {
  731. typedef CInstruction<T> super;
  732.  
  733. public:
  734. CInstructionJumpS()
  735. : CInstruction<T>("jumps"), m_addr("")
  736. {}
  737.  
  738. CInstructionJumpS *factory()
  739. {
  740. return new CInstructionJumpS;
  741. }
  742.  
  743. void compile(std::list<std::string>& params);
  744. void execute(CCPU<T> *cpu);
  745.  
  746. protected:
  747. /** labelname */
  748. std::string m_addr;
  749. };
  750.  
  751. /*----------------------------------------------------------------------------*/
  752.  
  753. template <class T>
  754. void CInstructionJumpS<T>::compile(std::list<std::string>& params)
  755. {
  756. if (params.size() != 1)
  757. throw CInstructionError("Invalid paramater count - must be 1");
  758. m_addr = params.front();
  759. params.pop_front();
  760. }
  761.  
  762. /*----------------------------------------------------------------------------*/
  763.  
  764. template <class T>
  765. void CInstructionJumpS<T>::execute(CCPU<T> *cpu)
  766. {
  767. assert(cpu != NULL);
  768. assert(cpu->getProgram() != NULL);
  769. if (!cpu->getFlagSign())
  770. return;
  771. if (m_addr.empty())
  772. throw CInstructionError("Empty address");
  773. try
  774. {
  775. cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr);
  776. }
  777. catch(CProgramError& ex)
  778. {
  779. throw CInstructionError(ex.what());
  780. }
  781. }
  782.  
  783. /*============================================================================*/
  784.  
  785. /**
  786.  * @class CInstructionWrite
  787.  *
  788.  * Implementation of assembler command "write"
  789.  * Syntax: write DEV, R1
  790.  * (write R1 to DEV, which is a name of a display)
  791.  */
  792. template <class T>
  793. class CInstructionWrite
  794. : public CInstruction<T>
  795. {
  796. typedef CInstruction<T> super;
  797. typedef typename std::set<CDisplay<T> *>::iterator setiterator;
  798.  
  799. public:
  800. CInstructionWrite()
  801. : CInstruction<T>("write"), m_dev("")
  802. {}
  803.  
  804. CInstructionWrite *factory()
  805. {
  806. return new CInstructionWrite;
  807. }
  808.  
  809. void compile(std::list<std::string>& params);
  810. void execute(CCPU<T> *cpu);
  811.  
  812. protected:
  813. /** register number */
  814. unsigned m_regidx1;
  815. /** device name */
  816. std::string m_dev;
  817. };
  818.  
  819. /*----------------------------------------------------------------------------*/
  820.  
  821. template <class T>
  822. void CInstructionWrite<T>::compile(std::list<std::string>& params)
  823. {
  824. if (params.size() != 2)
  825. throw CInstructionError("Invalid paramater count - must be 2");
  826. m_dev = params.front();
  827. params.pop_front();
  828. m_regidx1 = super::parseRegister(params.front());
  829. params.pop_front();
  830. }
  831.  
  832. /*----------------------------------------------------------------------------*/
  833.  
  834. template <class T>
  835. void CInstructionWrite<T>::execute(CCPU<T> *cpu)
  836. {
  837. assert(cpu != NULL);
  838. super::checkRegister(cpu, m_regidx1);
  839. if (m_dev.empty())
  840. throw CInstructionError("Empty device");
  841.  
  842. CDisplay<T> *display = NULL;
  843. std::set<CDisplay<T> *> displays = cpu->getDisplays();
  844. for(setiterator it = displays.begin(); it != displays.end(); ++it)
  845. {
  846. if ((*it)->getName() == m_dev)
  847. {
  848. display = *it;
  849. break;
  850. }
  851. }
  852. if (display == NULL)
  853. throw CInstructionError("Unknown display");
  854.  
  855. display->display(cpu->getRegisters()[ m_regidx1 ]);
  856. }
  857.  
  858. #endif
  859.  
  860. /* vim: set et sw=2 ts=2: */
  861.