Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Little Man Computer - Simulator Applet  (Read 17835 times)
0 Members and 1 Guest are viewing this topic.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Posted 2011-09-22 21:54:01 »

If you're nerdy like me, you might be into low level programming, which means fiddling with instructions. Unfortunately, the complexity easily spirals out of control, hence developing in a simple instruction-set is a great way to learn how a CPU works, and being able to write tiny applications that get things done.

An excellent example of such a language is Little-Man-Computer. It features a CPU with 1 register, 1 input, 1 output, 1 flag and 100 memory slots. It is minimalistic to the point that it lacks instructions for multiplication and division.


Specification of the instruction set:
1xxADD xxADDAdd the value stored in mailbox xx to whatever value is currently on the accumulator.
2xxSUB xxSUBTRACTSubtract the value stored in mailbox xx from whatever value is currently on the accumulator.
3xxSTA xxSTOREStore the contents of the accumulator in mailbox xx.
5xxLDA xxLOADLoad the value from mailbox xx and enter it in the accumulator.
6xxBRA xxBRANCH (unconditional)Set the program counter to the given address (value xx).
7xxBRZ xxBRANCH IF ZERO (conditional)If the accumulator contains the value 0, set the program counter to the value xx.
8xxBRP xxBRANCH IF POSITIVE (conditional)If the accumulator is 0 or positive, set the program counter to the value xx.
901INPINPUTGo to the INBOX, fetch the value from the user, and put it in the accumulator
902OUTOUTPUTCopy the value from the accumulator to the OUTBOX.
000HLTHALTStop working.
xxxn/aDATAInstruction which simply loads the value into the next available mailbox.


Simple program: (adds 13 and 14)
addressinstrcodedescription
#0104ADD 4add value of address #4 to register
#1105ADD 5add value of address #5 to register
#2902OUTwrite register to output
#30HLThalt execution of program
#413DATA: 13store the value 13
#514DATA: 14store the value 14

Obviously you can make (conditional) jumps, which allows you to make loops.

I created an applet which simulates this CPU, for you guys to play around. It really gets fun, once you get the hang of it.

[applet archive=lmc_ui.jar class=eden.emulator.lmc.LmcApplet width=512 height=256]

First thing you can do is trying to read two numbers from INPUT, subtract them, and write them to the OUTPUT. Next you can write code that is able to multiply two numbers. Maybe you feel naughty and try to raise N to the power of M. You'll need to use self-modifying code, seriously.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline theagentd
« Reply #1 - Posted 2011-09-23 00:16:21 »

Weird...

Myomyomyo.
Online princec

JGO Kernel


Medals: 364
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2011-09-23 00:43:33 »

It's 2am nearly here so I'm not going to try it yet but I love the new applet stuff in JGO Smiley

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #3 - Posted 2011-09-23 05:08:26 »

I see potential of great competitions.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2011-09-23 06:56:40 »

When I fix the copy/paste functionality of the JTextArea... we can actually share code persecutioncomplex

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2011-09-23 18:43:47 »

When I fix the copy/paste functionality of the JTextArea... we can actually share code persecutioncomplex
Seems like this is caused by the sandbox: you cannot copy & paste to/from the app. Shocked Is this new behaviour?

Edit: Right... copy/paste is deemed a security threat since 6u24 Lips Sealed

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Cero
« Reply #6 - Posted 2011-09-23 19:19:30 »

When I fix the copy/paste functionality of the JTextArea... we can actually share code persecutioncomplex
Seems like this is caused by the sandbox: you cannot copy & paste to/from the app. Shocked Is this new behaviour?

Edit: Right... copy/paste is deemed a security threat since 6u24 Lips Sealed

Yeah...
http://www.java-gaming.org/topics/is-it-possible-to-get-text-out-of-an-unsigned-applet/24506/msg/206538/view.html#msg206538
http://blogs.oracle.com/kyle/entry/copy_and_paste_in_java

It's weird.
And stupid I might add.

Offline lhkbob

JGO Knight


Medals: 32



« Reply #7 - Posted 2011-09-23 19:24:23 »

So how long have we been able to embed applets in the forum? Is it a ninja feature?

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2011-09-23 21:07:53 »

So how long have we been able to embed applets in the forum? Is it a ninja feature?
Developing it now... the only thing that's missing is a safe way to upload the JARs.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2011-09-23 21:36:24 »

Back ontopic:

I added syntax highlighting and highlighted the line of code currently executed, which is very convenient when developing stuff.



This is the code that calculates N to the power of 1,2,3,4,5,... etc (until it reaches 999, which is the maximum value of a memory cell).

You can see how you can define labels (multiply:) and use labels (:multiply)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
INP // fetch 'base' value from user
STA :base
init: LDA :base
STA :countdown

multiply: LDA 91 // :base = @90, 91 is next
ADD 90
BRP :nxt // did we overflow ?
LDA :zero
STA 91 // fill last addess with zero
HLT
nxt: STA 91
LDA :countdown // subtract one from countdown
SUB :one
STA :countdown
BRZ :display // reached zero in 'countdown'
BRA :multiply // back

display: LDA 91
OUT
BRA :selfmod

selfmod: LDA 4 // advance LDA & STA instructions
ADD :one
STA 4
LDA 5
ADD :one
STA 5
LDA 8
ADD :one
STA 8
LDA 10
ADD :one
STA 10
LDA 16 // for display
ADD :one
STA 16
BRA :init

stop: HLT

@90 base: 0
one: 1
end: 0
countdown: 0
zero: 0

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2011-09-26 01:48:55 »

Ok I am in need of some explanations:
1. What is a register?
2. What do you mean by input, output and flag?

Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #11 - Posted 2011-09-26 03:23:45 »

Ok I am in need of some explanations:
1. What is a register?
2. What do you mean by input, output and flag?

registers are a type of memory location inside the processor.
You typically only interact or learn about them when doing assembly programming and whatnot.

common assembly code would be like
add $s0, $t0, $t1    # this would be equivalent to s0=t0+t1
the 3 variables shown are all registers
But in this case there are 3 registers instead of 1

It essentially will go from hard drive to memory(ram) to cpu's registers(cpu's memory) and then often times back and forth between cpu's memory and ram's memory quite a bit in a typical application.

I also, if I am not mistaken, the input/outputs are simply  input from user, and output to user.

I am really fuzzy on all of it though, so I am sorry Riven if I explained incorrectly.


His program is in my opinion/mind is a very basic processor emulator in a low level (high level being C/C++/Java/etc..).   One of the classes in computer science in college, sometimes called computer organization you learn about assembly or MIPs and get to design a basic CPU from the ground up. lots of binary/hex/registery/ and pain in the ass assembly code.



"Experience is what you get when you did not get what you wanted"
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #12 - Posted 2011-09-26 03:30:12 »

This all sounds really really really really fun! I'm gonna be playing around with this for hours on end.

@Riven
Could you please post the source code of your Little Man Computer applet?

Offline ReBirth
« Reply #13 - Posted 2011-09-26 03:56:32 »

This is what I (should) learn in college, thanks Cheesy

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #14 - Posted 2011-09-26 08:03:19 »

I also, if I am not mistaken, the input/outputs are simply  input from user, and output to user.

In the LMC spec, the input/output is poorly specified (I assume it is asynchronous), so if something is outputed twice, and you don't read the OUTPUT values (externally) fast enough, you'll lose data, and if you change the data in INPUT before the CPU has read them, they are lost too.

To keep it maintainable, I made both INPUT and OUTPUT blocking, with a modal dialog.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #15 - Posted 2011-09-26 08:48:38 »

@Riven
Could you please post the source code of your Little Man Computer applet?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
public class Lmc
{
   public static final int HLT                  = 0;
   public static final int ADD                  = 1;
   public static final int SUB                  = 2;
   public static final int STA                  = 3;
   public static final int LDA                  = 5;
   public static final int BRA                  = 6;
   public static final int BRZ                  = 7;
   public static final int BRP                  = 8;
   public static final int UIO                  = 9;

   private int             program_counter      = 0;
   private int             execute_counter      = 0;
   private int             last_reference       = 0;
   private int             last_program_counter = 0;
   private int[]           memory               = new int[100];
   private int             boxInput             = 0;
   private int             boxOutput            = 0;
   private int             register;
   private boolean         overflowFlag         = false;

   public Lmc()
   {
      this.init();
   }

   public void load(int[] memory)
   {
      if (memory.length != this.memory.length)
      {
         throw new IllegalStateException("expected " + this.memory.length + " memory values");
      }

      for (int i = 0; i < memory.length; i++)
      {
         if (memory[i] < 0 || memory[i] > 999)
            throw new IllegalStateException("illegal memory value @" + i + ": " + memory[i]);
         this.memory[i] = memory[i];
      }

      this.init();
   }

   public void init()
   {
      this.reset();
      this.execute_counter = 0;
   }

   public void reset()
   {
      this.boxInput = 0;
      this.boxOutput = 0;
      this.register = 0;
      this.overflowFlag = false;
      this.program_counter = 0;
      this.last_program_counter = -1;
      this.last_reference = -1;
   }

   public int getInstructionPointer()
   {
      return this.program_counter;
   }

   public int getLastInstructionPointer()
   {
      return this.last_program_counter;
   }

   public int getLastReference()
   {
      return this.last_reference;
   }

   public boolean getOverflowFlag()
   {
      return this.overflowFlag;
   }

   public int readInbox()
   {
      return this.boxInput;
   }

   public int readOutbox()
   {
      return this.boxOutput;
   }

   public int readRegister()
   {
      return this.register;
   }

   public int readMemory(int address)
   {
      return this.memory[address];
   }

   public int execute(EmulatorFeedback feedback) throws EmulatorCrash
   {
      // fetch instruction
     int data = this.memory[this.program_counter];
     
      this.last_program_counter = program_counter;

      // advance program counter
     this.program_counter += 1;
      this.execute_counter += 1;

      // decode instruction
     int instruction = data / 100;
      int arg = data % 100;
      this.last_reference = arg;

      switch (instruction)
      {
         case HLT:
            this.last_reference = -1;
            if (arg != 0)
            {
               feedback.invalidInstruction();
               throw new EmulatorCrash();
            }
            this.reset();
            feedback.halt();
            break;

         case ADD:
            int doAdd = this.memory[checkAddress(arg, feedback)];
            this.register = normalizeRegister(register + doAdd);
            break;

         case SUB:
            int doSub = this.memory[checkAddress(arg, feedback)];
            this.register = normalizeRegister(register - doSub);
            break;

         case LDA:
            this.register = this.memory[checkAddress(arg, feedback)];
            break;

         case STA:
            this.memory[checkAddress(arg, feedback)] = this.register;
            break;

         case BRA:
            this.program_counter = checkAddress(arg, feedback);
            break;

         case BRZ:
            if (this.register == 0)
               this.program_counter = checkAddress(arg, feedback);
            break;

         case BRP:
            if (!this.overflowFlag)
               this.program_counter = checkAddress(arg, feedback);
            break;

         case UIO:
            this.last_reference = -1;
            switch (arg)
            {
               case 1:
                  this.boxInput = feedback.input();
                  this.register = this.normalizeRegister(this.boxInput);
                  break;

               case 2:
                  this.boxOutput = this.register;
                  feedback.output(this.boxOutput);
                  break;

               default:
                  feedback.invalidInstruction();
                  throw new EmulatorCrash();
            }
            break;

         default:
            break;
      }

      return data;
   }

   private int normalizeRegister(int value)
   {
      final int max = 1000;

      this.overflowFlag = (value < 0 || value >= max);

      return ((value % max) + max) % max; // ensure value between 0 and 999
  }

   private static int checkAddress(int addr, EmulatorFeedback feedback) throws EmulatorCrash
   {
      if (addr < 0 || addr >= 100)
      {
         feedback.invalidAddress(addr);
         throw new EmulatorCrash();
      }
      return addr;
   }
}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #16 - Posted 2011-09-27 04:45:57 »

Oh it's in pure Java? Then why did you need to sign it?

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #17 - Posted 2011-09-27 04:50:44 »

To enable copy & paste Angry

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2011-09-27 04:59:16 »

Stupid Oracle Angry

Note: You can't copy/paste from Flash apps either so this is not just Java. I don't get it, what's "not secure" about simple text?

Offline ReBirth
« Reply #19 - Posted 2011-09-27 12:06:02 »

Very interesting. I have a little naughty idea in my mind right now~ Cool
Do you want to release it as full open-source?

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #20 - Posted 2011-09-27 13:28:15 »

Very interesting. I have a little naughty idea in my mind right now~ Cool
Do you want to release it as full open-source?
Nope, not like that. If you have specific plans, PM me and I'll make up my mind.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Mike

JGO Wizard


Medals: 74
Projects: 1
Exp: 6 years


Java guru wanabee


« Reply #21 - Posted 2011-09-27 15:14:42 »

I don't get it, what's "not secure" about simple text?

Paste: If you have anything from passwords to bank accounts in your clipboard the program will be able to pick it up.
Copy: Overwrite something important you have in your clipboard without asking you. Smiley

Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline theagentd
« Reply #22 - Posted 2011-09-27 23:16:48 »

I don't get it, what's "not secure" about simple text?

Paste: If you have anything from passwords to bank accounts in your clipboard the program will be able to pick it up.
Copy: Overwrite something important you have in your clipboard without asking you. Smiley

Mike

I can't help thinking: Worrying about our ctrl-c-ed porn links, are we?


Myomyomyo.
Offline ReBirth
« Reply #23 - Posted 2011-09-28 12:07:49 »

Very interesting. I have a little naughty idea in my mind right now~ Cool
Do you want to release it as full open-source?
Nope, not like that. If you have specific plans, PM me and I'll make up my mind.
I will but not soon. My idea is kinda same as this, but different goal.

Offline Cero
« Reply #24 - Posted 2011-09-28 15:01:36 »

I don't get it, what's "not secure" about simple text?

Paste: If you have anything from passwords to bank accounts in your clipboard the program will be able to pick it up.
Copy: Overwrite something important you have in your clipboard without asking you. Smiley

Mike

First: with those arguments, you could ban copy/paste entirely from an OS
second: whatever is in the clipboard isn't very safe. one of the browsers, if you copy a link, then close it, clipboard is empty
Quote
If you have anything from passwords to bank accounts in your clipboard
Obviously - so don't be that stupid.

Offline sproingie

JGO Kernel


Medals: 202



« Reply #25 - Posted 2011-09-28 15:46:37 »

Passwords are on my clipboard all the time, for either a limited time or for the duration of one paste, because I use a password manager that generates passwords like this: jGHt%6j#]KJ,Xsh%2

I hardly think using a proper password manager is stupid.  The real problem is that there isn't any consumer OS with the concept of a secure clipboard.
Offline Mike

JGO Wizard


Medals: 74
Projects: 1
Exp: 6 years


Java guru wanabee


« Reply #26 - Posted 2011-09-28 16:02:03 »

Obviously - so don't be that stupid.

You'd be surprised with how little most computer users think about security Smiley

Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #27 - Posted 2011-09-28 20:39:32 »

Saykureti? What's that?

Offline johnkershaw

Senior Newbie





« Reply #28 - Posted 2012-09-02 16:54:42 »

Sorry to necropost, but I'm VERY interested in using your Little Man Computing app with my students this term, and I suspect others may too.

I'm switching to teaching GCSE Computing this year, from ICT. One of the programming research components has students use LMC to investigate 'bottom line' computing, learning how computers shift bits around, seeing how memory is used at low-level, etc. Unfortunately, the LMC app they recommend:

http://www.atkinson.yorku.ca/~sychen/research/LMC/LittleMan.html

doesn't work very well in any of my (Mac) browsers, specifically the text boxes are too small and only display one character, not much use when the box contain '901' and all I can see is a centre '0'. Definitely not good for students putting screenshots into reports...

So I went looking for an alternative LMC app... and there aren't any! The *idea* is well documented, but implementations are thin on the ground. Yours is excellent, especially the syntax colouring and single-stepping with line highlight. Unfortunately, I'm new to Java and can't figure out how to get this to be a standalone app, even though you kindly posted the source. I downloaded Eclipse, did the intro-tutorials, then pasted your code as a new project, but both 'checkAddress' and 'execute' are marked with red crosses, I *think* because 'EmulatorCrash cannot be resolved to a type'. Do I need to load a library/module/something to make this work? I did google 'EmulatorFeedback' and 'EmulatorCrash' but the only results point back to your page, which makes me think this is something specific to your code that I'm missing.

Alternatively, can I download the finished .jar file somehow? Not as good as having the source to go through with the students, but better than having to have them load the LMC from the discussion page!

John.

PS No offence, but the anti-spam sign-up requirements for this BB are mighty steep for someone just wanting to ask a casual question!
Offline jonjava
« Reply #29 - Posted 2012-09-02 21:24:44 »

We use MIPS in our uni, it's made in pure java too.

http://courses.missouristate.edu/kenvollmar/mars/

[EDIT]: Oh you want to run it directly in the browser? Nvm then.

But if you run it as an applet only isn't it hard to save and load previous code snippets?

Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (12 views)
2014-08-28 18:26:30

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (37 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (29 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (28 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!