Managed Code Toy OS
-
Upload
adhokshaj-mishra -
Category
Documents
-
view
222 -
download
0
Transcript of Managed Code Toy OS
-
7/31/2019 Managed Code Toy OS
1/35
Department of Computer Science and Engineering,
University Institute of Engineering and Technology,
Chhatrapati Sahu Ji Maharaj University
Managed Code Toy OSB. Tech Minor Project Report
Adhokshaj Mishra (29021)
-
7/31/2019 Managed Code Toy OS
2/35
Page 2 of35
Managed Code Toy
OSProject Report Submitted in Partial Fulfillment of
The Requirements for the Degree of
Bachelor of Technology
In
Computer Science and Engineering
By
Adhokshaj Mishra (Roll No. 29021)
Department of Computer Science and Engineering
University Institute of Engineering and
Technology,
CSJM University
Kanpur
-
7/31/2019 Managed Code Toy OS
3/35
Page 3 of35
Acknowledgement
I wish to express my deep sense of gratitude to my guide, Dr. Amit Anil
Sohara, Head of R&D Wing, Global Artificial Solutions for his able guidance and
useful suggestions, which helped me in completing the project work, in time.
Words are inadequate in offering my thanks to the Project Assistants, G.A.S.
for their encouragement and cooperation in carrying out the project work.
Finally, yet importantly, I would like to express my heartfelt thanks to my
beloved parents for their blessings, my friends for their help and wishes for the
successful completion of this project.
Adhokshaj Mishra
Department of Computer Science and Engineering,
University Institute of Engineering and Technology,
CSJM University
Kanpur
-
7/31/2019 Managed Code Toy OS
4/35
Page 4 of35
Table of Contents
oAbstract5oChapter 1: Introduction..6oChapter 2: Interfacing PS/2 Pointing Device.8oChapter 3: Virtual File System.15oChapter 4: Graphical User Interface.21oResult and Snapshots27oAppendix A: PS/2 Commands..30
-
7/31/2019 Managed Code Toy OS
5/35
Page 5 of35
Abstract
From the development of general purpose computers, software
industry has also evolved. However, there is not much improvementin OS development, as compared with other fields of software world:
while other fields are enjoying power of garbage collection, OS
developers are still doing their job in classic languages like C, C++
and assembly. Although there has been some research work
regarding this, none of them was successful. This project is an
attempt to develop a toy OS in pure managed code, particularly C#
and .NET Framework 4.0.
-
7/31/2019 Managed Code Toy OS
6/35
Page 6 of35
Chapter1: Introduction
In whole history of computers, operating systems are most challenging
and complex piece of software ever developed. Despite the complexity,very little efforts are made to make whole OS designing process easier,
and as a result, operating systems are still developed in C, C++ and
assembly language. Although modern languages like C# have made
software development process much easier, they cant be directly used
for OS development due to the fact they are compiled to intermediate
language (Byte-code or IL), instead of machine language for underlying
architecture. Since development of GC1
based languages, various
efforts have been made to harness the power of GC in OS developmentlifecycle. Some notable efforts in this direction are as follows:
Java OS
Java OS was the first attempt to develop an OS purely in managed code
(Java in particular). It was announced by Sun Microsystems in 1996, and
was claimed to run in anything from net computers to pagers. In 1998,
IBM was hired to accelerate the project. The end of project wasdeclared in 1999 by both companies, and was declared a legacy system
in early 2003.
Singularity
Singularity is an experimental OS by Microsoft Research started in
2003. It was designed as a highly-dependable OS in which the kernel,
device drivers, and applications are all written in managed code (C# inparticular). However, C/C++ code has been used
2for executing
managed code. This project was closed in 2010 and released in public
domain under restricted licensing terms.
1Garbage collection based languages like JAVA and C#. These languages provide automatic memory management.
2Source code is available athttp://singularity.codeplex.com/SourceControl/changeset/view/45126
http://singularity.codeplex.com/SourceControl/changeset/view/45126http://singularity.codeplex.com/SourceControl/changeset/view/45126http://singularity.codeplex.com/SourceControl/changeset/view/45126http://singularity.codeplex.com/SourceControl/changeset/view/45126 -
7/31/2019 Managed Code Toy OS
7/35
Page 7 of35
MOSA
Managed Operating System Alliance (MOSA3) is an open source OS
development tool-chain by developers mainly from USA, Germany and
Netherlands. This project provides command line tools to compiledmanaged libraries (.NET Framework) to native code, boot loader
configuration, ISO image generation etc.
COSMOS Framework
COSMOS (C# Open Source Managed Operating System) Project
provides a framework and tool-chain to develop an OS purely in
managed code (C# and VB .NET). Cosmos supports integration with
Visual Studio, and therefore is much easier to use. However, the
framework is not rich, and therefore, there is still a lot of work to be
done on OS developer side (E.g., it does not support mouse, file system
etc).
In this project, a microkernel4
based OS is developed using COSMOS
framework for x865
based computer systems. This project
demonstrates device driver (PS/2 pointing device), and a GUI system.
3http://www.mosa-project.org/
4In microkernel architecture,
5COSMOSE does not support x64 architecture yet.
http://www.mosa-project.org/http://www.mosa-project.org/http://www.mosa-project.org/http://www.mosa-project.org/ -
7/31/2019 Managed Code Toy OS
8/35
Page 8 of35
Chapter 2: Interfacing PS/2 Pointing
Device
Pointing devices (mouse or touch-pad) play a key role in user
interaction with GUI. They provide point-and-click mechanism to
eliminate use of keyboard for a large number of tasks. However, to
make them work in a computer, we need special software to
communicate with the device. This special software is known as device
driver, as it drives the hardware properly. Operating system interacts
with the driver, and all the low level communication is handled by the
driver.
System Architecture
-
7/31/2019 Managed Code Toy OS
9/35
Page 9 of35
Pointing devices are connected via two6
bi-directional tri-state lines:
Port60 and Port64. Port64 is used for signal7
traffic, and Port60 is used
for data traffic. Since Cosmos allows direct manipulation of these ports,
writing a basic driver is fairly simple, provided PS/2 communication
protocol is known (ref: appendix).
Device Acknowledgements
Pointing devices send two acknowledgements: for data and signal.
Before attempting to send or transmit any command/data, we have to
wait for proper acknowledgement. Also, a timeout mechanism is
required to prevent the driver from forever waiting state. Following
code tracks acknowledgements:
privatevoid WaitData(){
for (int i = 0; i < 100 & ((BaseIOGroups.Mouse.p64.Byte & 1) == 1); i++){
;}
}
privatevoid WaitSignal(){
for (int i = 0; i < 100 & ((BaseIOGroups.Mouse.p64.Byte & 2) != 0); i++){
;}
}
A timeout of 100 cycles8
has been used in both cases.
Device I/O
Device I/O consists of reading and writing over ports for
communication with device. Before we read something, we shouldmake sure that data is present. Following code reads data from port:
privatebyte Read()
6There is only one physical port for communication. Cosmos provides an abstraction layer for easy driver
programming.7
Signals are alias for notifications.8Cycle should not be confused with CPU cycle. Here cycle is used for loop cycles.
-
7/31/2019 Managed Code Toy OS
10/35
Page 10 of35
{WaitData();returnBaseIOGroups.Mouse.p60.Byte;
}
Writing over port is similar: send WRITE command (0xD4) over Port64,
and after acknowledgement, write data over Port60. Code snippet forthis is as follows:
privatevoid Write(byte b){
WaitSignal();BaseIOGroups.Mouse.p64.Byte = 0xD4;WaitSignal();BaseIOGroups.Mouse.p60.Byte = b;
}
Device InitializationMouse is initialized only once in whole system uptime, i.e., it is not hot-
pluggable. Initialization is performed in three steps:
Handshake Load default values Enable data reporting
Handshake is performed as follows:WaitSignal();BaseIOGroups.Mouse.p64.Byte = (byte)0xA8;WaitSignal();BaseIOGroups.Mouse.p64.Byte = (byte)0x20;byte status = (byte)(BaseIOGroups.Mouse.p60.Byte | 2);WaitSignal();BaseIOGroups.Mouse.p64.Byte = (byte)0x60;WaitSignal();BaseIOGroups.Mouse.p60.Byte = status;
Loading mouse with default values is straightforward: issue 0xF6command over Port60. Similarly, to enable data reporting issue 0xF4
command and set interrupt handler to parse data packets sent by
mouse.
-
7/31/2019 Managed Code Toy OS
11/35
Page 11 of35
Movement Data Packet
The standard PS/2 mouse sends movement and button information to
the host using the following 3-byte packet:
Bit 7 Bit 6Bit
5
Bit
4Bit 3 Bit 2 Bit 1 Bit 0
Byte
1
Y
overflow
X
overflow
Y
sign
bit
X
sign
bit
Always
1
Middle
Button
Right
Button
Left
Button
Byte2
X movement
Byte
3Y movement
The movements values are 9-bit 2's complement integers, where the
most significant bit appears as a "sign" bit in byte 1 of the movement
data packet. Their value represents the mouse's offset relative to itsposition when the previous packet was sent, in units determined by the
current resolution. The range of values that can be expressed is -255 to
+255. If this range is exceeded, the appropriate overflow bit is set.
Now, we can write a parser to extract data, as follows:
privateint _x;
privateint _y;publicMouseState Buttons;privatereadonlybyte[] _mouseByte = newbyte[4];privatebyte _mouseCycle = 0;
publicenumMouseState{
None = 0,Left = 1,Right = 2,
-
7/31/2019 Managed Code Toy OS
12/35
Page 12 of35
Middle = 4}
switch (_mouseCycle){
case 0:
_mouseByte[0] = Read();
//Bit 3 of byte 0 is 1, then we have a good packageif ((_mouseByte[0] & 0x8) == 0x8)
_mouseCycle++;
break;case 1:
_mouseByte[1] = Read();_mouseCycle++;break;
case 2:_mouseByte[2] = Read();_mouseCycle = 0;
if ((_mouseByte[0] & 0x10) == 0x10){
_x -= (_mouseByte[1] ^ 0xff);}else{
_x += _mouseByte[1];}
if ((_mouseByte[0] & 0x20) == 0x20){
_y += (_mouseByte[2] ^ 0xff);}else{
_y -= _mouseByte[2];}
if (_y < 0){
_y = 0;}if (_x < 0){
_x = 0;}
Buttons = (MouseState)(_mouseByte[0] & 0x7);
break;}
-
7/31/2019 Managed Code Toy OS
13/35
Page 13 of35
Since we will interact with our driver to use mouse services, there
should be a public API. At bare minimum, methods to check mouse
button, and to retrieve mouse coordinates will be required.
Implementation of API is as follows:
publicint X{
get { return _x; }}
publicint Y{
get { return _y; }}
publicBoolean CheckLeftClick(){
if (Buttons == MouseState.Left){
returntrue;}else{
returnfalse;}
}
publicBoolean CheckRightClick(){
if (Buttons == MouseState.Right)
{returntrue;
}else{
returnfalse;}
}
publicBoolean CheckMiddleClick(){
if (Buttons == MouseState.Middle){
returntrue;}else{
returnfalse;}
}
publicBoolean CheckNoClick()
-
7/31/2019 Managed Code Toy OS
14/35
Page 14 of35
{if (Buttons == MouseState.None){
returntrue;}else
{ returnfalse;}
}
BibliographyChapweske, A. (2003, 01 04). PS/2 Mouse Interfacing. Retrieved from Computer Engineering:
http://www.computer-engineering.org/ps2mouse/
Furman, J. O. (2001). Rapid Prototyping of Digital Systems.
Howei, J. (2009, 01 30). PS2 Commands. Retrieved from Altium:
http://wiki.altium.com/display/ADOH/PS2+Commands
-
7/31/2019 Managed Code Toy OS
15/35
Page 15 of35
Chapter 3: Virtual File System
File systems provide a way to store data arranged in files and folders,
on some storage media like optical disc, magnetic disc, or RAM 9. The
file system implemented in this toy OS is virtual: it uses RAM as its
storage media. This file system emulates a real file system. Various FS
elements are discussed below:
Directory
Directory is implemented using a class with single data member: path,
as shown below:
publicclassDirectory{
//the path of the directory.publicstring path;
#region " Directory Handlers "
public Directory(){
//Blank Directory.}
public Directory(string p){
this.path = p;}
#endregion " Directory Handlers "}
File
A file in the toy OS holds actual contents and some metadata in same
unit. Metadata includes file name, size in bytes, type or extension, and
path of directory to which it belongs. Implementation is as follows:
9RAM is used as storage media in live operating systems. Live operating systems work without installation on
physical hard disc.
-
7/31/2019 Managed Code Toy OS
16/35
Page 16 of35
publicstring name = "";publicstring filetype = "";publicint size = 0;publicstring data = "";publicDirectory path = newDirectory();
Since size of file should be updated each time file is edited, a public
method is provided to update the same:
publicvoid SetSize(){
this.size = this.data.Length;}
DiskA disk is a container which physically holds data in a computer system.
Since total no. of files is dynamic, disk is implemented using List
which is technically a linked list. Also, each disk is tagged with a driver
letter10
. To emulate mounting feature of LINUX family, swapping is also
supported, where new disk is inserted in place of old disk with same
driver letter11
. Basic implementation is as follows:
publicList Files = newList();publicstring Driveletter = "";
public Disk(){}
public Disk(string letter){
this.Driveletter = letter;}
publicvoid Start(string letter){this.Files.Clear();this.Driveletter = letter;
}
10It is similar to driver letters in Windows family of OS.
11Please refer to file system implementation for this feature.
-
7/31/2019 Managed Code Toy OS
17/35
Page 17 of35
publicvoid Start(){
this.Files.Clear();}
The file I/O system supports adding/removal of file(s), disk cleanup,
formatting, and file editing. Code listing for it is given below:
publicvoid AddFile(File newfile){
this.Files.Add(newfile);}
publicvoid AddFile(string name, string data, Directory path){
File newfile = newFile();
newfile.name = name;newfile.data = data;newfile.path = path;newfile.SetSize();Files.Add(newfile);
}
publicvoid AddFile(string name, string data){
File newfile = newFile();newfile.name = name;newfile.data = data;newfile.SetSize();
Files.Add(newfile);}
publicvoid AddFile(string name, string data, string type){
File newfile = newFile();newfile.name = name;newfile.data = data;newfile.filetype = type;newfile.SetSize();Files.Add(newfile);
}
publicvoid RemoveFile(int i){Files.RemoveAt(i);
}
publicvoid RemoveFile(string name){
for (int i = 0; i < Files.Count; i++){
if (this.Files[i].name == name)
-
7/31/2019 Managed Code Toy OS
18/35
Page 18 of35
{Files.RemoveAt(i);break;
}}
}
publicvoid RemoveFile(int start, int end){
for (int i = start; i < end; i++){
Files.RemoveAt(i);}
}
publicvoid PartitionDisk(){
this.Files.Clear();}
publicvoid CleanUpFiles(int sizeamount){
for (int i = 0; i < Files.Count; i++){
this.Files[i].SetSize();if (this.Files[i].size
-
7/31/2019 Managed Code Toy OS
19/35
Page 19 of35
}}return"";
}
publicbool SetFileContents(string nme, string newdata)
{ for (int i = 0; i < Files.Count; i++){
if (this.Files[i].name == nme){
Files[i].data = newdata;Files[i].SetSize();returntrue;
}}returnfalse;
}
File System
A file system is implemented as a container of disks, as a single file
system can span up to multiple disks. It supports only three operations:
adding, removal, and swapping of disks. Implementation is pretty
straightforward:
publicstaticList Disks = newList();privatestaticint i;
publicvoid AddDisk(Disk newdisk){
Disks.Add(newdisk);}
publicvoid RemoveDisk(string letter){
for (i = 0; i < Disks.Count; i++){
if (Disks[i].Driveletter == letter){
Disks.RemoveAt(i);break;
}}
}
publicvoid RemoveDisk(int i){
Disks.RemoveAt(i);}
-
7/31/2019 Managed Code Toy OS
20/35
Page 20 of35
publicvoid SwapDisks(Disk NewDisk, string letter){
for (i = 0; i < Disks.Count; i++){
if (Disks[i].Driveletter == letter)
{ Disks[i] = NewDisk;break;
}}
}
To lookup for a particular disk search by driver letter is supported.
publicDisk GetDisk(string letter){
for (i = 0; i < Disks.Count; i++){
if (Disks[i].Driveletter == letter){
return Disks[i];}
}returnnewDisk();
}
Hard DiskIn the toy OS, a hard disk can contain only one file system within
itself12. Hard disk is implemented as follows:
publicclassHardDrive{
publicstaticFilesystem Drive = newFilesystem();}
12In other words, hard disk partitioning is not supported.
-
7/31/2019 Managed Code Toy OS
21/35
Page 21 of35
Chapter 4: Graphical User Interface
Graphical User Interface or GUI is probably easiest way to interact with
a computer. Therefore, almost every modern OS provides a rich GUI to
let the user explore the world more easily13
.
Since COSMOS framework does not support any GUI natively, whole
graphics system is to be developed by the OS developer. Fortunately,
the framework support graphics mode in which it lets the programmer
to color any pixel on the screen, and therefore, GUI can be drawn using
standard computer graphics algorithms. A sample snapshot ofimplemented GUI with various components is shown below:
Currently, GUI supports resizable windows, drag and drop, and
overlapping windows. However, it does not support variable z index,
i.e. foreground window cannot be put in background or vice versa. Core
13One possible exception is BACKTRACK operating system, in which most of the work is have to be done through
terminal or console. There is no GUI support for many tools blended with the OS.
-
7/31/2019 Managed Code Toy OS
22/35
Page 22 of35
GUI is implemented in a single class window, which encapsulates all
required functions. The code for implementation is as follows:
public window(uint x, uint y, uint Width, uint Height, uint color, window p, bool modal)
{this.X = x;this.Y = y;this.Width = Width;this.Height = Height;this.Color = color;this.Parent = p;p.Child = this;this.Modal = modal;this.Ch = 0U;Array.Clear((Array)this.Str, 0, this.Str.Length);window.LastWnd = this;
}
publicstaticwindow Active(uint x, uint y, window lWnd){
if (x > lWnd.X && x < lWnd.X + lWnd.Width && (y > lWnd.Y && y < lWnd.Y +lWnd.Height))
window.ActiveWnd = lWnd;elseif (lWnd.Parent != null)
window.Active(x, y, lWnd.Parent);returnwindow.ActiveWnd;
}
publicstaticvoid ReDraw(window fWnd){
fWnd.Draw();if (fWnd.Child == null)
return;window.ReDraw(fWnd.Child);
}
publicvoid Draw(){
uint num1 = 0U;uint num2 = 0U;VGAScreen vgaScreen = newVGAScreen();for (uint index = this.X; index < this.X + this.Width; ++index){
uint num3;for (num3 = this.Y; num3 < this.Y + this.Height; ++num3){
if ((int)index == (int)this.X || (int)num3 == (int)this.Y)vgaScreen.SetPixel320x200x8(index, num3, 8U);
elsevgaScreen.SetPixel320x200x8(index, num3, this.Color);
vgaScreen.SetPixel320x200x8( this.X + this.Width, num3, 8U);}
-
7/31/2019 Managed Code Toy OS
23/35
Page 23 of35
vgaScreen.SetPixel320x200x8(index, num3, 8U);}if (!this.Modal){
this.DrawBar();this.DrawCorner();
num2 += 5U;}}
publicvoid DrawBar(){
VGAScreen vgaScreen = newVGAScreen();for (uint index = this.X; index < this.X + this.Width; ++index){
uint num;for (num = this.Y; num < this.Y + 5U; ++num){
if ((int)index == (int)this.X || (int)num == (int)this.Y)vgaScreen.SetPixel320x200x8(index, num, 8U);
elsevgaScreen.SetPixel320x200x8(index, num, 3U);
vgaScreen.SetPixel320x200x8( this.X + this.Width, num, 8U);}vgaScreen.SetPixel320x200x8(index, num, 8U);
}}
publicvoid DrawCorner(){
VGAScreen vgaScreen = newVGAScreen();uint num = 0U;for (uint index1 = (uint)((int)this.X + (int)this.Width -
1); index1 > (uint)((int)this.X + (int)this.Width - 4); --index1){
for (uint index2 = (uint)((int)this.Y + (int)this.Height -3) + num++; index2 < this.Y + this.Height; ++index2)
vgaScreen.SetPixel320x200x8(index1, index2, 3U);}
}
publicuint Status(uint mx, uint my){
if (my < this.Y + 5U && mx < (uint)((int)this.X + (int)this.Width - 5))return 1U;
if (my < this.Y + 5U && mx > (uint)((int)this.X + (int)this.Width - 5))return 2U;
if (mx > (uint)((int)this.X + (int)this.Width -4) && mx (uint)((int)this.Y + (int)this.Height -4) && my
-
7/31/2019 Managed Code Toy OS
24/35
Page 24 of35
publicstaticvoid Destroy(window d, window pWnd){
d = d.Parent;d.Child = (window)null;window.LastWnd = d;window.ReDraw(pWnd);
}
publicvoid Sync(uint mx, uint my, window pWnd){
this.X = mx;this.Y = my;window.ReDraw(pWnd);
}
publicvoid SynSz(uint mx, uint my, window pWnd){
this.Width = mx;this.Height = my;window.ReDraw(pWnd);
}
publicvoid StringTo(string s){
this.Str = s.ToCharArray();}
To use the mouse, we need to interact with the hardware through
device driver implemented in chapter 2. Mouse is attached to GUI as
shown below:
publicstaticvoid Initialize(Cosmos.Hardware. VGAScreen scr, window parentWnd){
Mouse.Hardware.Mouse Mouse2 = new Mouse.Hardware.Mouse();
Mouse2.Initialize();
uint x = (uint)Mouse2.X;uint y = (uint)Mouse2.Y;
uint oc = 0, oc1 = 0, oc2 = 0, oc3 = 0;
uint k = 0;
window nd = newwindow(10, 20, 150, 50, 5, window.LastWnd, false);
window end = newwindow(parentWnd.Width / 2, parentWnd.Width / 2, 50, 30,5, nd, false);
end.StringTo("DIALOG WND");
-
7/31/2019 Managed Code Toy OS
25/35
Page 25 of35
window wnd = null; window act = null;
window.ReDraw(parentWnd);
while (true){
#region Mouse Data
uint mx = (uint)Mouse2.X;uint my = (uint)Mouse2.Y;
if (mx != x || my != y){
scr.SetPixel320x200x8(x, y, oc);scr.SetPixel320x200x8(x, y + 1, oc1);scr.SetPixel320x200x8(x + 1, y, oc2);scr.SetPixel320x200x8(x + 1, y + 1, oc3);x = mx;y = my;oc = scr.GetPixel320x200x8(x, y);oc1 = scr.GetPixel320x200x8(x, y + 1);oc2 = scr.GetPixel320x200x8(x + 1, y);oc3 = scr.GetPixel320x200x8(x + 1, y + 1);//Mouse2.Draw();
}
switch (k){
case 1:act.Sync(mx, my, parentWnd);break;
case 2:window.Destroy(act, parentWnd);k = 0;break;
case 3:act.SynSz(mx, my, parentWnd);break;
case 4:window l = window.LastWnd;if (l.Ch == 666) window.Destroy(l, parentWnd);k = 0;break;
}
scr.SetPixel320x200x8(mx, my, 6);scr.SetPixel320x200x8(mx, my + 1, 6);scr.SetPixel320x200x8(mx + 1, my, 6);scr.SetPixel320x200x8(mx + 1, my + 1, 6);
if (Mouse2.CheckRightClick()){
window last = window.LastWnd;
if ((last.Modal == true) && (last.Ch == 666))
-
7/31/2019 Managed Code Toy OS
26/35
Page 26 of35
{window.Destroy(last, parentWnd);
}
wnd = newwindow(0, 0, 51, 51, oc, window.LastWnd, true);
wnd.Color = 3;wnd.Ch = 666;wnd.StringTo("CREATE WND");wnd.Sync(mx, my, parentWnd);
}
if (Mouse2.CheckLeftClick()){
act = window.Active(mx, my, window.LastWnd);
if (act.Modal == true){
if (act.Ch == 666){
window newWnd = newwindow(parentWnd.Width / 3, parentWnd.Width / 3, 50, 30, 5, act.Parent, false);
}elseif (act.Ch == 0){
k = 4;}else{
nd.CharTo(act.Ch - 1);}
}else{
k = act.Status(mx, my);}
window.ReDraw(parentWnd);}
if (Mouse2.CheckMiddleClick()){
k = 0;}
#endregion Mouse Data}
}
-
7/31/2019 Managed Code Toy OS
27/35
Page 27 of35
Result
The developed microkernel based toy OS boots successfully in VMWare
Player, and all implemented functionalities work flawlessly. It should
boot on any x86 based real machine, however it is not guaranteed.
Snapshots
-
7/31/2019 Managed Code Toy OS
28/35
Page 28 of35
-
7/31/2019 Managed Code Toy OS
29/35
Page 29 of35
-
7/31/2019 Managed Code Toy OS
30/35
Page 30 of35
Appendix A: PS/2 Mouse Commands
Commands Sent by Host to Mouse
Code Description and Mouse Action
E6h This command is used to set the current scaling from 2:1 to 1:1.
After receipt of the command, the mouse replies with the
acknowledge code (FAh) and changes the scaling to be 1:1.
E7h This command is used to set the current scaling from 1:1 to 2:1.
After receipt of the command, the mouse replies with the
acknowledge code (FAh) and changes the scaling to be 2:1.
E8h This command is used to set the resolution for the mouse. The
mouse sends an acknowledgement (response code FAh) and
then waits for another byte of data from the host, that specifies
the resolution to be used, as follows:
00h 1 count per millimeter
01h 2 counts per millimeter
02h 4 counts per millimeter
03h 8 counts per millimeter
After receipt of this second byte, the mouse sends the
acknowledge code again and resets its movement counters
E9h This command is used to request the current status of the
-
7/31/2019 Managed Code Toy OS
31/35
Page 31 of35
mouse. Upon receipt of the command, the mouse sends the
acknowledge response code (FAh), followed by a three byte
status packet, structured as follows:
Byte1
Bit0 State of right mouse button (1=pressed; 0=not pressed)
Bit1 State of middle mouse button (1=pressed; 0=not pressed)
Bit2 State of left mouse button (1=pressed; 0=not pressed)
Bit3 Not Used (set to 0)
Bit4 Current scaling in use (1= 2:1; 0= 1:1)
Bit5 Data reporting state (1=enabled; 0=disabled)Bit6 Current mode (1= Remote mode; 0= Stream mode)
Bit7 Not Used (set to 0)
Byte2 Current resolution in use
Byte3 Current sampling rate in use
After sending the status packet, the mouse resets its movement
counters.
EAh This command is used to set the mouse in Stream mode. The
mouse responds by sending the acknowledge code (FAh),
resetting its movement counters and entering Stream mode. In
this mode, a packet of data is sent every time the mouse detects
movement, or when one of its buttons has changed state. Data is
sent to the host, providing that data reporting is enabled. The
frequency of packet transmission is determined by the sampling
rate the default being 100 samples per second.
EBh This command is used to read sampled data from the mouse
-
7/31/2019 Managed Code Toy OS
32/35
Page 32 of35
whilst it is in Remote mode. The mouse responds by sending the
acknowledge response code (FAh) and then sending its current
packet of movement data. The movement counters are
subsequently reset.
ECh This command is used to reset Wrap mode. The mouse responds
by sending the acknowledge code (FAh), resetting its movement
counters and entering the mode that it was in prior to entering
Wrap mode (either Stream or Remote).
EEh This command is used to set the mouse in Wrap mode. Themouse responds by sending the acknowledge code (FAh),
resetting its movement counters and entering Wrap mode. In
this mode, the mouse echoes all commands directly back to the
host, without additional or further response. Two exceptions to
this are the commands to reset (FFh) and reset Wrap mode
(ECh). In these cases, the mouse responds as per the entries for
these commands in this table.
F0h This command is used to set the mouse in Remote mode. The
mouse responds by sending the acknowledge code (FAh),
resetting its movement counters and entering Remote mode. In
this mode, the inputs to the mouse (movement and buttons) are
still sampled, but no packets of data are sent to the host.
F2h This command is used to read the mouse ID. Upon receipt, the
mouse responds with the acknowledge response code (FAh),
followed by the code that represents its ID and distinguishes it as
a standard PS/2 mouse 00h. The movement counters are also
-
7/31/2019 Managed Code Toy OS
33/35
Page 33 of35
reset at this time.
F3h This command is used to set the sampling rate for the mouse
(when monitoring its movement and button inputs). The mousesends an acknowledgement (response code FAh) and then waits
for another byte of data from the host, that specifies the
sampling rate to be used, as follows:
0Ah 10 samples per second
14h 20 samples per second
28h 40 samples per second
3Ch 60 samples per second
50h 80 samples per second
64h 100 samples per second
C8h 200 samples per second.
After receipt of this second byte, the mouse sends the
acknowledge code again and resets its movement counters.
F4h This command is used to enable data reporting when the mouse
is in Stream mode. Upon receipt of the command, the mouse
sends the acknowledge response code (FAh) and resets its
movement counters. It continues to sample its inputs
(movement and buttons) and packets of data are once again
sent to the host.
F5h This command is used to disable data reporting when the mouse
is in Stream mode. Upon receipt of the command, the mouse
sends the acknowledge response code (FAh) and resets its
movement counters. It continues to sample its inputs
-
7/31/2019 Managed Code Toy OS
34/35
Page 34 of35
(movement and buttons) but no packets of data are sent to the
host. Stream mode with data reporting disabled is analogous to
the mouse being in Remote mode.
F6h This command is used to load the mouse with default values.
Upon receipt of the command, the mouse sends the
acknowledge response code (FAh) and then loads the following:
Sampling Rate 100 samples per second Resolution 4 counts per millimeter Scaling 1:1 Data Reporting Disabled.
These default values are also loaded when a reset command is
received from the host, or when the mouse is powered-up (as
part of its Basic Assurance Test). After loading the values, the
mouse resets its movement counters and enters Stream mode.
FEh This is the resend command and is used when the host requires
the mouse to retransmit the last packet of data sent. Upon
receipt of the command, the mouse sends the acknowledge
response code (FAh), then proceeds to transmit the previously
sent packet of data (e.g. movement data, status information,
BAT code, ID, etc).
FFh This command is used to reset the mouse. Upon receipt of the
command, the mouse sends the acknowledge response code
(FAh). The mouse is reset and subsequently performs its power-
on Basic Assurance Test (BAT). During this test, default values
-
7/31/2019 Managed Code Toy OS
35/35
Page 35 of35
are loaded for the sampling rate, resolution and scaling, and data
reporting is disabled. Depending on the result of the test, the
mouse will either send the 'Passed' code (AAh) or the 'Failed'
code (FCh).
After the result of the power-on test is sent, the mouse sends its
ID (00h). The mouse will then enter Stream mode. No movement
data packets will be sent however, until the host first sends the
command to enable data reporting.
Commands Sent by PS/2 Device
Code Description
00h Mouse ID
AAh Power-on Basic Assurance Test Passed
FCh Power-on Basic Assurance Test Failed
FAh Acknowledge
FEh Resend. Upon receipt of this code, the PS2 Controller will
retransmit the previous byte.